Wednesday, 4 March 2009

Counting the Number of Sessions Per Citrix Server

Whilst monitoring some newly provisioned Citrix servers running on VMware hosts today, I soon became very bored with manually checking how many sessions were on each Citrix VM as the load on each one increased, whilst trying to get it to the optimum level.

I knew it was possible to use Powershell to connect with Citrix servers, but had never really looked into it before. Not surprisingly it turned out to be very straightforward.

By using some technology known as MFCom we can connect with the Citrix farm and get some cool information out.

In the below example we create a new com object using MFCom, then initialise the connection. We are then able to access some methods and properties of that object. In this case we are looking at the Sessions property, we group all of the results by ServerName and then produce some output with the name and number of sessions on that Citrix box.

$farm = New-Object -com "MetaframeCOM.MetaframeFarm"
$farm.Sessions | Group-Object ServerName | Sort-Object name | Format-Table Name,Count -auto
which will give you something like:

Name Count

CitrixServer01 38
CitrixServer02 45
CitrixServer03 41

This would return all of the servers in the farm. In this particular instance I only wanted a particular selection of servers, so I stored them in a text file, got PS to read that file and then filter the query by only looking at servers in that list.

$servers = Get-Content c:\scripts\servers.txt

$farm = New-Object -com "MetaframeCOM.MetaframeFarm"
$farm.sessions | Where-Object {$servers -contains $_.ServerName} | Group-Object ServerName | Sort-Object name | Format-Table Name,Count -auto

Once again Powershell very easily gets rid of a really dull manual task.

If you wish to take this a step further check out Powershell MVP Brandon Shell's blog where he has loads of Powershell / Citrix examples.


vishal ganeriwla said...

Hi Jonathan
My name is Vishal Ganeriwala and I manage the Citrix Developer Network Program. This is a great blog post. You can find more examples on

Please feel free to contact me if you have any questions regarding MFCOM APIs.

Jonathan Medd said...

Thanks, will check it out.