This post is part of a 6 part series. Jump to [part 2] [part 3] [part 4] [part 5] [part 6].
Overview: There’s quite a bit of data that we can gather via queries to MFCom. With this series I want to demonstrate how to use PowerShell, MS SQL, SQL Reporting Services and Visual Studio to gather real-time stats and present them in a dashboard that is easy to read and even easier to present to management.
I’m presenting today an edited PowerShell script that I grabbed from http://synjunkie.blogspot.com/2008/12/powershell-retrieving-useful-citrix.html. Syn is also writing a hacking Citrix series of articles that is proving to be very interesting.
Here’s the original 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 $<span class="livesessions">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
This script is useful, but I found that it took several minutes to run in our environment due to iterating through all our Citrix sessions 3 times. To speed the script up and ensure the data did not change as the script was running I loaded everything into an array and then replicated the data output.
$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
Now the script runs 3 times as fast.
Thanks,
Alain
How did you insert the script code into the blog? I’ve been trying to get that for my wordpress blogs. Appreciate your help in this regard.
I recently found that WordPress fully supports Alex Gorbatchev’s SyntaxHighlighter plug-in.
You can get more information from this WordPress Support page: http://en.support.wordpress.com/code/posting-source-code/
Thank you for reading,
Alain
Great article! Wondering though, is it possible to insert a check for the creation of the metaframe-object, e.g. when the datastore is unavailable the script will still run and report 1 user.
I think this is a good idea, but if your datastore isn’t available, you’ve got bigger problems :).
Was wondering if it is possible to subscribe to your post feed through email.
Yes. I’ve added a place on the left hand side to subscribe via e-mail. It’s under the RSS feed subscriptions.
Thanks,
Alain
[…] 1 : Making Citrix Stats Work for You Part 1 Part 2 : Making Citrix Stats Work for You Part 2 Part 3 : Making Citrix Stats Work for You Part 3 […]
[…] PowerShell to gather some specific session stats from Citrix MFCom and output them to a text file. [part 1] [part […]