Monday 30 March 2009

Reboot-Servers

The Need:
After patches have been applied to my servers I like to control the reboot schedule. To do this I break the servers down into groups of 10 or so and run the following script against each list.


The Script:
Get-Content c:\servers.txt | ForEach-Object { gwmi win32_operatingsystem -ComputerName $_ | ForEach-Object { $_.reboot() } | out-null ; write-host "$_ is going down"}

Get-RemoteCD

The Need:
When patching my servers I occasionally have an issue that I reboot a server that has a CD left in the drive and the server doesn't come back up after the reboot. I quickly wrote this script to check through a list of servers for CD Drive that have disks inserted.


The Script:
#This script checks a list of remote server to see if a CD is loaded.
$servers = Get-Content c:\servers.txt
function Get-RemoteCD
{
Foreach ($Server in ($Servers))
{
gwmi win32_cdromdrive -comp $Server | select SystemName,MediaLoaded,VolumeName | ft -auto
}}
Get-RemoteCD

Saturday 28 March 2009

Count-CitrixSessions

The Need:
Throughout the day I like to get stats on my citrix farm to make sure everything is ticking over okay. This script isn't as fast as it could be and I hope to revise it at a later date. For this script I needed to install the free citrix SDK from the citrix site.



The Script:
#Count-CitrixSession.ps1
#count citrix sessions and other useful info

$farm = new-Object -com "MetaframeCOM.MetaframeFarm"
$farm.Initialize(1)

# displays a list of published apps and the number of users on each
write-host "Total users on each citrix application" -fore yellow
$farm.sessions | select UserName,AppName | group AppName | Sort Count -desc | select Count,Name | ft -auto
$livesessions = ($farm.Sessions).count
write-host "The number of current citrix sessions is" $livesessions -fore red

write-host " "

# list of citrix servers and total number of sessions on each one
write-host "Total sessions on each citrix server" -fore yellow
$farm.sessions | select ServerName,AppName | group ServerName | sort name | select Count,Name | ft -auto

write-host " "

# To see which users have more than one session open
write-host "First 15 Users with more than one citrix session" -fore yellow
$farm.sessions | select UserName,AppName | group UserName | Sort Count -desc | select Count,Name -first 15 | ft -auto



The Update:
I found this update of my script on the WagTheReal blog. The author has edited the script to make it run about 3 times faster. He has also followed up his post with a series of posts that import the results of this into SQL. Very cool stuff indeed!


$livesessions = 0
$disconnected = 0
$farm = New-Object -com "MetaframeCOM.MetaframeFarm"
$farm.Initialize(1)

#Load Up Array for a snapshot of current sessions in CITGO
$sessionAry = @($farm.Sessions | select UserName,AppName,ServerName,SessionState)

foreach ($sess in $sessionAry) {
if ($sess.SessionState -eq "5") {$disconnected = $disconnected + 1}
else {$liveessions = $livesessions++}
}

Write-Host "The number of active citrix sessions is" $livesessions -fore red
Write-Host "The numbrer of disconnected citrix sessions is” $disconnected -fore red

Write-Host ” "

# displays a list of published apps and the number of users on each
Write-Host “Total users on top 20 citrix applications” -fore yellow
$sessionAry | group AppName | sort Count -desc | select Count,name -first 20 | ft -auto

Write-Host ""

# list of citrix servers and total number of sessions on each one
write-host “Total sessions on each citrix server” -fore yellow
$sessionAry | group ServerName | sort name | select Count,Name | ft -auto

write-host ""

# To see which users have more than one session open
write-host "First 20 Users with more than one citrix session" -fore yellow
$sessionAry | group UserName | Sort Count -desc | select Count,Name -first 20 | ft -auto


Get-NewUsers

The Need:
I have a need to know what accounts have been created within the past week/month.



The Script:
#Get-NewUsers
#This script will list new accounts and then count them.

$days = (read-host "How many days back shall I go?")
Write-host "This script will display all accounts created in the past $days days. Please wait a few minutes" -fore yellow
$date = get-date
$date = $date.AddDays(-$days)
write-host " "
$TotalAccountsCreated = Get-QADUser -SizeLimit 0 | where {$_.whenCreated -ge $date} | select Name,Company
$TotalAccountsCreated
write-host " "
Write-host "There have been" $totalaccountscreated.count "accounts created in the past $days days" -fore red

Introduction

I have created this blog so I can separate my Powershell posts from the security related posts that I make on my SynJunkie blog.

The scripts I'll be posting here are going to be simple scripts that I have put together that help me in my job as a Systems Administrator. There won't be anything too fancy and I'm sure there will be many other ways to accomplish the same task. These scripts are just purely what has worked for me at the time.

The format of the posts will be, what problem I am trying to solve and the script I have used.

Whilst getting started with scripting I have had loads of help from the guys at the Powershell Community forums and I would recommend any new user should take a look there. The PowerScripting Podcast is also an excellent source of information.