One of the first things that I tried to do when learning the XenDesktop PowerShell SDK was to figure out how to retrieve (and set) the various XenDesktop settings that you find within the Delivery Services Console (DSC). The focus of this blog will be to show how you can retrieve and set the XenDesktop farm properties. The approach that I will outline here can be carried over into retrieving the other various XenDesktop settings such as Desktop Delivery Controller settings, Desktop Group settings, etc.

This is the second blog in a series on how to use the XenDesktop 4 PowerShell SDK. In the first blog, I provided info on how to set up your XenDesktop PowerShell environment. If you haven’t done that yet, please visit that article first. For a complete list of topics that I will be covering in this series, see the bottom of this article.

XenDesktop PowerShell Objects
The key to retrieving and setting XenDesktop settings is to use the XenDesktop PowerShell objects that are provided by the XenDesktop PowerShell snap-in XDCommands. So what are the XenDesktop objects? To get a complete list of the XenDesktop PowerShell objects, type the following commands at the PowerShell prompt:

#Add the XenDesktop snap-in to the current Powershell session
Add-PSSnapin <span class="code-quote">"XdCommands"</span>

#View the XenDekstop SDK help file
Get-Help about_XdCommands

The Get-Help about_XdCommands command above will print out the entire SDK help file. I would definitely recommend reviewing this entire file to get a better feel for what the SDK is all about. In case you are curious, you can also look at the text file version of this content by navigating to C:\Program Files\Citrix\Desktop Delivery Controller\Powershell\ on the machine where you have the XenDesktop SDK installed. Whether you execute the above command or view the text file, you want to go to the section called Citrix Desktop Delivery Controller Administration Object Classes. This section shows you all 12 XenDesktop objects that you can leverage within the PowerShell SDK (shown below).

So our goal here is that we want to retrieve the XenDesktop farm properties. In looking at the above list, the XdFarm object represents farm-wide status and settings. So we know we need to get a reference to the XdFarm object. That’s where the XenDesktop cmdlets come in.

XenDesktop PowerShell Cmdlets
In the first blog of this series, I mentioned that you can type the following command to retrieve a complete list of all the XenDesktop PowerShell cmdlets.

Get-Command -PSSnapin <span class="code-quote">"XdCommands"</span>

Within the cmdlet list above, there are several that start with either Get or New. These cmdlets are the ones that you want to use for creating the XenDesktop PowerShell objects mentioned above. Since we are trying to create a XdFarm object for retrieving the XenDesktop farm settings, the Get-XdFarm cmdlet looks like the one we need. To verify this cmdlet returns an object of type XdFarm, we want to view the help on this specific cmdlet. Type the following command at the PowerShell prompt.

Get-Help Get-XdFarm -Full

Within the cmdlet help, look for the section called Return Type. In the picture above, the Return Type section does state that this cmdlet returns an object of type XdFarm. So we know we found the right cmdlet.

Retrieving the XenDesktop farm properties
We now know that the Get-XdFarm cmdlet will return an object of type XdFarm, which represents the status and settings of the XenDesktop farm. Let’s now look at the entire PowerShell script for retrieving (and setting) the farm properties. Then we’ll take a closer look at certain parts of it for those that want some additional information.

#Retrieve and set the XenDesktop farm properties

#Add the XenDesktop snap-in to the current Powershell session
Add-PSSnapin 'XdCommands'

#Retrieve the XenDesktop farm properties from the DDC (specified via IP address)
$xdfarm = Get-XdFarm -AdminAddress

#(Optional) List all properties/methods of the XdFarm object
$xdfarm | get-member

#Get XenDesktop farm properties
echo $xdfarm.BaseOU
echo $xdfarm.Edition
echo $xdfarm.EnableSessionReliability
echo $xdfarm.LicenseServerName
echo $xdfarm.LicenseServerPort
echo $xdfarm.Name
echo $xdfarm.SessionReliabilityDurationSeconds
echo $xdfarm.SessionReliabilityPort

#Set XenDesktop farm license server name
$xdfarm.LicenseServerName = 'NewLicServer'
Set-XdFarm $xdfarm

#Set XenDesktkop farm session reliability duration
$xdfarm.SessionReliabilityDurationSeconds = 120
Set-XdFarm $xdfarm

Analyzing the PowerShell Script
The script above demonstrates how to both retrieve the farm properties and set a few properties such as License Server name and Session Reliability Duration. I’m storing the output of the Get-XdFarm command into a variable called $xdfarm. This variable is an object of type XdFarm. To make sure the Get-XdFarm command worked, you can display the value of the $xdfarm variable using echo $xdfarm. Since this variable is an object, it will also have properties and methods that we can call. To get a list of them, you can use the get-member cmdlet as shown below. I would definitely recommend using get-member on any variable that represents an object as you start developing your own PowerShell scripts. It’s a great way to learn what datatype the object is, as well as what properties and methods it supports.

#List out the current contents of the $xdfarm variable
echo $xdfarm

#Provide a complete list of properties/methods supported by the XdFarm object
$xdfarm | get-member

In the picture above, we can see the words get;set; or just get; listed at the end of each property. This tells us whether the property is Read-Only (get;) or Read/Write (get;set;). If a property is Read-Only, you can only display the contents of that property via the echo command. If the property is Read/Write, you can also set the value of the property. To set the value of a XenDesktop farm property, we need to leverage the Set-XdFarm cmdlet as shown below.

$xdfarm.LicenseServerName = 'NewLicServer'
Set-XdFarm $xdfarm

To summarize, the Get-XdFarm cmdlet will give us the XdFarm object to view the settings of the XenDesktop farm. If we want to set the values of the farm, we need to use the Set-XdFarm cmdlet. Please note that not all properties within a XdFarm object can be set. They need to be designated as Read/Write in order for a property to be configured . This same theme holds true with the several of the other XenDesktop cmdlets and objects, so feel free to do some extra exploration on your own.

In the next article, I will discuss how to automate the creation a new desktop group. We laid a lot of ground work with this article, so the next article will be more focused on just the script itself. Stay tuned!

Upcoming TechTalk
I will be leading a TechTalk with Mike Bogobowicz on Essentials for using Windows PowerShell with XenApp and XenDesktop on Tuesday, August 24 from 2pm to 3pm EST. If you interesting in learning more about these SDKs first hand and want to see the demos in action, you can sign up here. We hope to see you there!

Blogs in this series

Ed York – Senior Architect – Worldwide Technical Readiness
Ask-the-Architect Site:
Follow Me on twitter: