Friday, 5 November 2010

Notes on PowerShell in XenApp 6

Powershell v2 comes installed by default on Windows Server 2008 R2.  Citrix have followed Microsoft’s example with products such as Exchange in providing a fairly thorough set of commands in Powershell to monitor and administer the farm.  And with the loss of MFCOM as a way of command line farm administration, its become an essential skill.  Personally I have no real need at the moment to use Powershell for actions such as publishing applications as our environment is still small enough to be faster to administer with the Delivery Services Console, but for any mass change to the farm the command line will become far faster and for monitoring, its great.
To easily be able to use PowerShell to administer XenApp 6, first you need to download and install the SDK.  And I like to have my admin tools as published apps, so this is how to create a XenApp 6 PowerShell based management application as a hosted application on your farm:
Publishing the PowerShell console with XenApp commands
  • Download the XenApp Powershell SDK :
  • Extract and execute the installer
  • Agree to change the execution level to AllSigned
  • Publish an application to this server with the following location:
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -File "c:\Program Files\Citrix\XenApp Server SDK\Citrix.XenApp.Sdk.ps1"
  • Launch your new editor
  • By default it will not be set up to recognise XenApp commands while editing, so use the command:
    Add-PSSnapin Citrix*

Publishing the PowerShell ISE Editor
You can use Notepad to edit your PowerShell scripts – but that’s not very fun so let’s publish an application “Windows PowerShell Integrated Scripting Environment (ISE)”. This is a very good editor for developing PowerShell scripts, with a nice window to run them in and some basics like automatic colouring of your code.  Of course, you can skip this if you like Notepad…
  • Login to your XenApp 6 server
  • Go to Command Prompt
  • Enter this command: 
    servermanagercmd -install PowerShell-ISE
  • It will whinge and then work anyway:
  • Publish a hosted application on your farm at:
  • Again, you will need to run the command “Add-PSSnapin Citrix*” to preload all the commands for XenApp.  In ISE, you can enter quick commands like this in the bottom window, then build up your scripts in the tabbed top window.  The middle window displays any console output.
Some useful basic commands for XenApp in PowerShell
I’m planning a much bigger post on this, but in the meantime, here’s some really basic bits of code to show you what you can do with PowerShell…
Display the farm name
$farm = Get-XAFarm   
Display the zone name data collector for a zone (the zone of the XenApp server you run on of course)
$zone = get-XAzone
Display counts for the total number of active and disconnected sessions in the farm
Get-XASession -Farm | where-object {$_.State -eq "Active" } | Measure-object
Get-XASession -Farm | where-object {$_.State -eq "Disconnected" } | Measure-object
Display a list of all your farm servers, listed alphabetically, with the number of sessions on each:
$servers = Get-XAServer -full   
$output = "`nServers: `n"
    foreach($server in $servers | sort-object ServerName){
        $output+=$server.ServerName + "  " + $server.SessionCount + "`n"
Remember if you are saving any of these its best to put the command “Add-PSSnapin Citrix*” at the top to make sure it always has the Citrix commands loaded.  You should really consider signing your scripts as well – which is an art in itself!


Post a Comment