As you manage your XenDesktop farm, you may notice that you have spikes in virtual desktop use every so often. In some cases the spikes could exceed the supply and some users could be rejected from receiving a virtual desktop. To deal with the spikes, you want your XenDesktop environment to be elastic – that is, as the threshold of what can be currently supported is being approached, to dynamically add new virtual desktops to a desktop group on the fly to deal with the increased demand.
This article provides information on how to automate the adding of new virtual desktops to an existing VM-based desktop group via a PowerShell script. The common use case for this is to deal with spikes in usage, but there can be other use cases as well. As new virtual desktops are added to the desktop group, you can also dynamically update the idle pool settings of the desktop group via PowerShell to adjust how many idle sessions there are at any given time. Check out my last blog for more information on how to use PowerShell to dynamically adjust the idle pool settings.
This is the sixth 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 so that you could run these scripts. 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 blog series, see the bottom of this article.
Our goal for the script is pretty simple: To dynamically add new virtual desktops to an existing VM-based desktop group via PowerShell. I found that when I wanted to add a single virtual desktop versus multiple virtual desktops that my script was slightly different. I’m providing both of them as part of this blog so that you can see the difference.
The picture below shows the current virtual desktops that I already have defined as part of my desktop group called “Windows XP”. We are going to add new virtual desktops to this list via the PowerShell scripts. The important concept that you should notice in this screen shot is that XenDesktop maintains a between the Virtual Machine name and Active Directory machine name for each virtual desktop within the desktop group. You will see this mapping concept represented in the PowerShell scripts below.
The new virtual desktops that I want to add are listed in the table below. One thing to keep in mind is that these virtual machines already have the Virtual Desktop Agent (VDA) software installed. The only thing they need is to be added to the desktop group so that users can start accessing them.
|Virtual Machine Name||Active Directory Machine Name||IP Address|
The sample script below demonstrates how to add a single virtual desktop to a VM-based desktop group.
The sample script below demonstrates how to add multiple virtual desktops to a VM-based desktop group.
After executing the script, you can open the Delivery Services Console and verify that the virtual desktops have been added to the desktop group:
If you are new to PowerShell, you would probably say that the first script for adding a single virtual desktop looks very easy, the second script for adding multiple virtual desktops looks much more complex. The good news on the second script is that I have all of the complex parts already coded for you. All you have to do is adjust the variables at the top to indicate your own environment information and virtual desktop list.
Essentially what both scripts are doing is using the Get-XdDesktopGroup cmdlet to get a reference to the desktop group we want to add the new virtual desktops to. In the past few blogs, I go into much more detail regarding how this cmdlet works.
Next, both scripts are using the Get-XdHostedMachine cmdlet to get a reference to the virtual machines on the hosting infrastructure (e.g. XenServer) that we are looking to add. In the first script, the $vm variable represents a single virtual desktop. In the second script, the $vms variable represents an array of virtual desktops.
Before we add the virtual desktops to the desktop group, we want to make sure the mapping exists between the virtual machine name and Active Directory machine name. That’s where use of the Name property of the virtual desktop object comes in. Notice the difference of how I am setting the Name property within each script.
After the mapping has been defined, we add the virtual desktops to the desktop group by calling the Add() method of the $xdgroup.Desktops collection. The Add() method can only accept a single virtual desktop at a time, that is why in the second script we are looping over the virtual desktops array ($vms variable) and adding them one-by-one.
Finally, we commit the change to the Desktop Delivery Controller (DDC) by calling the Set-XdDesktopGroup cmdlet.
This article explains how to add new virtual desktops to an existing VM-based desktop group. A common use case for doing this is to deal with spikes in usage where the surge would exceed the existing virtual desktop supply. On the flip side, you can easily adjust these scripts to remove virtual desktops from a desktop group once the number of sessions is going down. Just use the Remove function on the $xdgroup.Desktops collection.
In the next blog, we will look at using PowerShell to add more AD users and groups to an existing desktop group. Stay tuned!
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. Feel free to also check out Mike’s blog on XenApp 6 PowerShell scripting here. We hope to see you at the TechTalk!
- Getting Started
- Retrieving the XenDesktop farm properties
- Creating a new desktop group
- Enabling/disabling a desktop group
- Updating the idle pool settings of a desktop group
- Adding virtual desktops to a desktop group (this one)
- Adding AD users and groups to a desktop group
- Disconnecting and stopping virtual desktop sessions
- Restarting virtual desktops and series wrap-up
Ask-the-Architect Site: http://community.citrix.com/p/product-automation#home
Follow Me on twitter: http://twitter.com/citrixedy