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


No comments: