Windows PowerShell cmdlets for XenServer management

You can make Citrix XenServer management easier by using Windows PowerShell cmdlets. The XenServer PowerShell snap-in is handy for automating tasks.

Some administrators find that Citrix Systems doesn’t have great support for Windows PowerShell, but a PowerShell snap-in can improve Citrix XenServer management. 

The XenServer PowerShell snap-in is a tool no XenServer admin can be without -- even if you are a religious XenClient or XenServer command line user. By adding this arsenal of PowerShell cmdlets to your existing Citrix PowerShell toolbox, you can manage Citrix XenDesktop, XenApp, Provisioning Server, Netscaler and XenServer all with a single command-line interface.

You can download the XenServer PowerShell snap-in from the Citrix SDK website. I also recommend downloading the XenServer Snapshots snap-in if you use snapshots for backup or disaster recovery in your infrastructure.

Setting up the PowerShell snap-in

The XenServer PowerShell snap-in is pretty easy to set up on a 32-bit client operating system. But to get Windows PowerShell working on a 64-bit system, such as Windows 7 or Windows Server 2008 R2, run the following command from an elevated command prompt to register the DLL with the 64-bit .NET Framework:

C:\windows\microsoft.net\frameowkr64\v2.0.50727\installutil.exe “c:\program files (x86)\citrix\xenserverpssnapin\xenserverpssnapin.dll”

A message will let you know when the XenServer PowerShell snap-in has registered. Then, add the snap-in to your PowerShell session or profile. To check if the snap-in is already loaded, add the following code to your Windows PowerShell profile:

if (((Get-PSSnapin -Name "XenServerPSSnapIn" -ErrorAction SilentlyContinue) -eq $null ) -and ((Get-PSSnapin –registered -Name "XenServerPSSnapIn") -ne $null))
{ Add-PSSnapin XenServerPSSnapIn
."C:\Program Files\Citrix\XenServerPSSnapIn\Initialize-Environment.ps1" }

Now that you’ve got the XenServer PowerShell snap-in loaded, run the command Connect-Xenserver to log in to your host as the root user.

PowerShell cmdlets for XenServer management

As with all Windows PowerShell modules and snap-ins, the Get-Command and Get-Help cmdlets are your best friends when you need to see syntax and examples.  These are some PowerShell cmdlets that I use frequently:

  • To create three VMs based off a template and specify their names (great for creating POC and lab environments):
    • Copy-LocalVM name1 name2 name3 1 3
  • To see what templates are available to use:
    • Get-Template
  • To find out who the master of the pool is:
    • Get-XenServer:Pool.Master
  • To set a home server for a VM:
    • Set-XenServer:VM.Affinity –VM <name> -Affinity <xenserver_name>
  • To set the license server for the host and the license edition:
    • Set-XenServer:Host.LicenseServer –server <xenserver> -Host xenserver -LicenseServer <license_host>
    • Invole-XenServer:Host.ApplyEdition –server <xenserver_name> -Host xenserver –Edition <enterprise, advanced>

By combining these XenServer PowerShell cmdlets into a single script, you can create your own automated environment for XenServer hosts and VMs.

This PowerShell script is a good example of ones you’ll find for public use. This Hypervisor Independent script is another useful one that works with both XenServer and VMware. Once you’re comfortable with XenServer PowerShell cmdlets, start creating your own scripts to create, change, report on and delete XenServer hosts and VMs.

One last cool thing to know about XenServer management with PowerShell is how to sort and interpret data that’s collected from the OS command line itself -- for instance, disk I/O usage. Fortunately, XenServer has the built-in Linux command iostat to show the actual disk I/O for the host and VMs over a period of time that you can specify. But the command isn’t very good at formatting its output data, and it simply sends the information to a text file.

Luckily, the Virtualization Jedi has come up with an awesome Windows PowerShell script to read those text files and format them as space-delimited files that you can open in Excel or Google Spreadsheets. From there, you can sort and analyze the data to your heart’s content.

With this new set of cmdlets added to your XenServer management toolbox, you could manage your entire infrastructure through Windows PowerShell.

More on XenServer management

Dig Deeper on Citrix XenServer