Scripting. Powershell, VMware, Windows, Active Directory & Exchange. All that kind of stuff…..
RSS icon Email icon Home icon
  • Counting the Number of Sessions Per Citrix Server

    Posted on March 4th, 2009 Jonathan Medd 5 comments

    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.Initialize(1)$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.Initialize(1)$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.

    http://bsonposh.com/archives/tag/citrix

     

    5 responses to “Counting the Number of Sessions Per Citrix Server” RSS icon

    • vishal ganeriwla

      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
      http://community.citrix.com/cdn/xa/codeshare

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

    • Jonathan Medd

      Thanks, will check it out.

    • Hello Jonathan congratulations for your awsome blog, may be you culd help me cose Im trying to do this same for Citrix session but with a particular diferences. Im runing powershell 2 from a server with XenApp 6 and im trying to get the ICA sessions for a farm with XenApp 4.5, Could you please provie me some lights to try to solve this? Thanks In Advance

    • Hi,

      I don’t think you will be able to do this from the XenApp 6 server because as far as I am aware the COM interface needed for 4.5 has been removed in 6. You need to run it from the XenApp 4.5 server, or maybe you could use PowerShell remoting to create the session?

      Jonathan

    • Thanks for your quick response, I’m afraid that you’re right about this and I will need to have installed powershell on the farm with 4.5 just had this doubt because even now I run scripts to check services and disk space from this server with XenApp 6 to all servers with 4.5, but that is more related to windows that citrix


    Leave a reply