Intro
I’ve been hesitant to dive into XenServer PowerShell cmdlets, but there’s no rational reason to not do it. Citrix continues to make great strides in expanding and updating PowerShell for XenServer, PVS, and XenDesktop. Today, we’ll go over a function that queries an array of XenServer Poolmasters and returns the total VM count on each. The idea behind this function was to stop manually counting VM’s in XenCenter and to understand VM growth and XenServer Pool utilization.
NOTE: Thanks to The Scripting Frog for getting me most of the way there with this function.

XenServer and PowerShell?
It may seem weird to use PowerShell to perform queries of a Linux-based system, but such is the world we live in. I remember back in my day :). Apple was a joke, IBM ruled the PC market and Linux didn’t exist. Of course I still remember saving BASIC programs to a cassette deck.
The script will prompt for credentials which can be root or any XenServer administrator. Then you connect to each pool master in turn…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Loop through list of hosts (poolmaster) | |
$xenserver_poolmaster | ForEach-Object { | |
# Connect to XenServer pool | |
Connect-XenServer –Server $_ –Creds $xenserver_credential –SetDefaultSession –NoWarnNewCertificates |
The important flags are -SetDefaultSession and -NoWarnNewCertificates. You must set the default session on each new XenServer connection, otherwise, the script will not know what pool master to query. The NoWarnNewCertificates flag prevents a prompt asking you to accept the new XenServer certificate (you can leave this out if you want this additional warning to let you know you’re connecting to a new XenServer).
Unless you can refer to your XenServers with a DNS name, you can do some quick translation to make your output more readable. I’m using a switch statement to replace the IP address with a XenServer name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
switch ($_) { | |
"192.168.50.1" {$xsn = "XenServerPool1"; break} | |
"192.168.50.2" {$xsn = "XenServerPool2"; break} | |
default {"UNKNOWN XENSERVER"; break} | |
} |
The rest is just getting all the VM’s (minus snapshots, templates, etc), counting them and putting the results into a custom PowerShell Object. Finally you disconnect from each XenServer and go to the next one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Retrieve the information | |
$XenServerVMs = Get-XenVM | Where-Object {$_.is_a_snapshot -eq $false -and $_.is_a_template -eq $false -and $_.is_control_domain -eq $false -and $_.power_state -eq 'running'} | Select-Object name_label | |
$vmCount = $XenServerVMs.count | |
$objctxsrv = new-object System.Object | |
$objctxsrv | Add-Member –type NoteProperty –name XenServer –value $xsn | |
$objctxsrv | Add-Member –type NoteProperty –name 'VM Count' –value ($vmCount) | |
$finalout += $objctxsrv | |
# Disconnect from the XenServer pool | |
Get-XenSession –Server $_ | Disconnect-XenServer |
The results…
XenServer VM Count --------- -------- XenServerPool1 108 XenServerPool2 109
You can get this and so much more from my github.
Thanks for reading,
Alain Assaf