Many of you may have seen my blog series last year on the XenDesktop 4 PowerShell SDK. Ever since XenDesktop 5 was released, I’ve been itching to take a peak at the new release, and in particular, the new SDK. I’ve had several recent projects where I had to create scripts with the XenDesktop 5 SDK. The majority of these projects involved automating several key aspects of a XenDesktop 5 configuration such as:

  • Creating hypervisor connections and hosts
  • Creating different types catalogs (Physical, MCS, PVS, etc.)
  • Creating desktop groups based on the catalogs

I’ve learned quite a bit about the SDK while on these projects, and I’m going to share my scripts and lessons learned within this blog series. I’ve only barely scratched the surface on what is possible with the SDK, but I hope the work I’ve done can help get you started with the type of automation that you are looking to do in your own environment.

In this first blog, I’m going to begin with how to get started and things you should know prior to using the SDK. This info will help set the foundation for the rest of the series…

Why use the XenDesktop 5 PowerShell SDK?
I can see a couple big reasons for using the XenDesktop 5 SDK. If you are in the business for setting up and tearing down large numbers of environments, automating this is the way to go. That’s actually how I got into using the SDK. I’m an architect for the Virtual Computing Demo Center, which is a cloud-based demo system. We needed to automate the XenDesktop 5 configuration on our cloud-based demos since we have to build these demos in the hundreds, the configuration procedure was the same for each, but the underlying machines/passwords/IP addresses were different. It was a perfect use case for using the SDK!

The second big reason for using the SDK is that the Desktop Studio Console (the admin console for XenDesktop 5) doesn’t expose all of the configuration settings that you can possibly do within XenDesktop 5. An example of this is creating pooled physical catalogs within XenDesktop 5. A physical catalog is a catalog that points to pre-existing virtual desktops. These virtual desktops could be physical devices (such as Blade PCs) or pre-existing VMs that have the Virtual Desktop Agent installed. If you create a “physical” catalog using the Desktop Studio Console, it’s going to automatically put the catalog into a assigned on first use state. Catalogs that are assigned on first use are different than pooled catalogs. With assigned on first use, new users will get assigned to the next available desktop, and the desktop assignment will stick on subsequent launches of the desktop. With pooled desktops, users get assigned to the next available desktop and that assignment is cleared upon logoff. On the next logon, they will get assigned to the next available desktop again which could be a different VM. In XenDesktop 5, to create a physical catalog that is pooled you need to create the catalog using the PowerShell SDK; the Desktop Studio Console doesn’t provide it as an option. I’m actually going to show you how to do this in one of the later blogs in this series!

First Impressions of the XenDesktop 5 SDK:
Quite simply, my first words were “Really extensive!”, “Much different than the XenDesktop 4 SDK!”, and “Where do I start???”. The XenDesktop 5 SDK has 100+ cmdlets now! When you think about how much XenDesktop 5 is different than XenDesktop 4 (and how much much the consoles are different), you’ll begin to understand the reason for the SDK changes.

So how do you start writing PowerShell scripts for XenDesktop 5? Thankfully Engineering put two key resources together that will help get you started.

First, the Desktop Studio Console provides the PowerShell output for the majority of actions that you perform within the console (see screen shot below). When I first heard about this feature of XenDesktop 5, I thought “Great! I can perform an action in the console, grab the PowerShell output from the Console, stick the commands into Notepad, and I’ll have my script”. As you review the PowerShell output in more detail, it unfortunately doesn’t work like that. The output doesn’t reference any variables that it created or used behind the scenes; it just gives you the output of the variables. So you have to do a little bit of investigative work to figure out how it achieved the output it is showing. It reminds me of a programming class I took in college – We were given the final program for free, but our “project” was to figure out the code that produced the final program. Even though the PowerShell output from the console doesn’t give you the full script, it’s still the best place to start when you want to write your own scripts. The way that I actually use the PowerShell output is to perform an action within the console such as creating a catalog. I then review the PowerShell output to gather the list of cmdlets it used to perform the action. I’ll then look up those cmdlets and experiment with them. I’ll then define my own variables that will help me to achieve the same result. As you dig into this output more you’ll see what I mean

Second, you’re going to want to bookmark the XenDesktop 5 cmdlet reference help pagehttp://support.citrix.com/static/kc/CTX127254/help/index.htm. This page lists all the PowerShell cmdlets for XenDesktop 5 and gives you information on what parameters they take, what they output, some examples, etc. I’ve used it quite a bit and it helps immensely! For those of you that like using the cmdlet help text within PowerShell (i.e. Get-Help <Cmdlet-Name>), the text between the two are very similar, but I think the web page may actually give more info for certain cmdlets and it is easier to read.

Developing Scripts with the XenDesktop 5 SDK
I’ve experimented with the XenDesktop 5 cmdlets a few different ways. The first approach I used was to launch the PowerShell window directly from the Desktop Studio Console (see screen shot above on how to access it). This approach is nice since this PowerShell window already has the XenDesktop 5 PowerShell snap-ins loaded into the session. You can start calling the XenDesktop cmdlets right away.

For developing scripts, I prefer using a free PowerShell script editor called PowerGui.exe. PowerGui is nice since once it loads the snap-ins that you need, you can get the Intellisense drop-down boxes much like with Microsoft Visual Studio. It really makes working with the cmdlets easier and it’ll tell you what parameters are available for each cmdlet. Plus, it’ll show you the current values of all your PowerShell variables on the right-hand side. Many thanks to Mike Bogobowicz for showing this to me last year!

Loading the XenDesktop 5 cmdlets to your PowerShell session
If you open a standard PowerShell window or use a script editor like PowerGui.exe for developing scripts, you are going to need to load the XenDesktop 5 cmdlets to your PowerShell session. I typically cut/paste the following code snippet at the top of the script to accomplish this. This code sample will first check if the XenDesktop 5 cmdlets are already loaded into the PowerShell session, and if not, load them.

$snapins = Get-PSSnapin | where { $_.Name -like <span class="code-quote">"Citrix*"</span> }
<span class="code-keyword">if</span> ($snapins -eq $<span class="code-keyword">null</span>)
{
   Get-PSSnapin -Registered <span class="code-quote">"Citrix*"</span> | Add-PSSnapin
   Add-PSSnapin <span class="code-quote">"PvsPsSnapin"</span>
}

Getting help on the XenDesktop 5 cmdlets
The easiest way I found help on the cmdlets is to use the XenDesktop 5 Cmdlet Reference Page as mentioned above. This page has a complete list of all the cmdlets with a detailed explanation on how to use them. If you have a PowerShell session already open, you can also use the built-in help. Just use the Get-Help cmdlet as noted below:

Get-Help &lt;Cmdlet-Name&gt;

For example:

Get-Help Get-BrokerCatalog

Many of the XenDesktop cmdlets return something back to you, such as an object, string, or integer (although most seem to be objects). When you get a reference to one of the special XenDesktop objects, it’s good to know what properties and methods it has. Use the Get-Member cmdlet to list the properties and methods for the objects that are returned. For example:

#Get reference to a catalog called <span class="code-quote">"Windows 7"</span> within XenDesktop
$objCatalog = Get-BrokerCatalog -Name <span class="code-quote">"Windows 7"</span>

#List all properties/methods of the Catalog object type
$objCatalog | Get-Member

#Display all current property values <span class="code-keyword">for</span> the <span class="code-quote">"Windows 7"</span> catalog
echo $objCatalog

#Display the <span class="code-quote">"Description"</span> property of the <span class="code-quote">"Windows 7"</span> catalog
echo $objCatalog.Description

Available Resources
It’s always good to know what resources are out there when learning a new SDK. If I missed any resources, feel free to post a comment and I’ll be sure to update my list…

Blogs in this series
I have several blogs planned for this series based on the recent projects that I’ve had. I’ll add more to this list as time allows.

Ed York – Senior Architect – Worldwide Technical Readiness
Ask-the-Architect Site: http://community.citrix.com/p/product-automation#home
Follow Me on twitter: http://twitter.com/citrixedy