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.

Script Goals
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 mapping 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
WINXP_XD4_VDA4 xpvda4 10.10.10.60
WINXP_XD4_VDA5 xpvda5 10.10.10.61

PowerShell script for adding a single virtual desktop to a VM-based desktop group
The sample script below demonstrates how to add a single virtual desktop to a VM-based desktop group.

#***********************************************************************
#Add a single virtual desktop to an existing VM-based desktop group
#***********************************************************************

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

#Set up variables <span class="code-keyword">for</span> the script
$strDDCAddress = <span class="code-quote">"10.10.10.56"</span>
$strDesktopGroupName = <span class="code-quote">"Windows XP"</span>
$strVMName = <span class="code-quote">"WINXP_XD4_VDA4"</span>         #This is the virtual machine name to add to the desktop group
$strADName = <span class="code-quote">"xpvda4"</span>                 #This is the corresponding Active Directory machine name

#Get a particular desktop group
$xdgroup = Get-XdDesktopGroup -Name $strDesktopGroupName -AdminAddress $strDDCAddress -HostingDetails

#Get the VM (virtual desktop) that we want to add to the desktop group
$vm = Get-XdHostedMachine -HostingServer $xdgroup.HostingSettings.HostingServer -Name $strVMName

#Create the mapping between virtual machine name and Active Directory machine name
#Note:  When I echo the $vm variable created above, the <span class="code-quote">"Name"</span> property of the virtual machine seems to be already set <span class="code-keyword">for</span> me.
#       This might be because the mapping between VM Name and AD Name has been cached from adding <span class="code-keyword">this</span> VM to other desktop groups on prior occasions.
#       Keeping the statement below won't cause any problems in any <span class="code-keyword">case</span> and it should not have to be modified.
#       Just make sure the Active Directory machine name <span class="code-keyword">for</span> <span class="code-keyword">this</span> virtual machine is correct as defined above.
$vm.Name = $strADName;

#Add the VM (virtual desktop) to the desktop group
$xdgroup.Desktops.Add($vm)

#Apply the change to the DDC
Set-XdDesktopGroup $xdgroup

#Verify the update
echo $xdgroup.Desktops

PowerShell script for adding multiple virtual desktops to a VM-based desktop group
The sample script below demonstrates how to add multiple virtual desktops to a VM-based desktop group.

#***********************************************************************
#Add multiple virtual desktops to an existing VM-based desktop group
#***********************************************************************

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

#Set up variables <span class="code-keyword">for</span> the script
$strDDCAddress = <span class="code-quote">"10.10.10.56"</span>
$strDesktopGroupName = <span class="code-quote">"Windows XP"</span>
$vmnames = <span class="code-quote">"WINXP_XD4_VDA4"</span>, <span class="code-quote">"WINXP_XD4_VDA5"</span>   #These are the virtual machine names to add to the desktop group
$adnames = <span class="code-quote">"xpvda4"</span>, <span class="code-quote">"xpvda5"</span>                   #These are the corresponding Active Directory machine names (make sure they align with the $vmnames array)

#Get a particular desktop group
$xdgroup = Get-XdDesktopGroup -Name $strDesktopGroupName -AdminAddress $strDDCAddress -HostingDetails

#Get the VMs (virtual desktops) that we want to add to the desktop group
$vms = Get-XdHostedMachine -HostingServer $xdgroup.HostingSettings.HostingServer | where  { $vmnames -contains $_.HostingName }

#Create the mapping between virtual machine name and Active Directory machine name
#Note:  When I echo the $vms variable created above, the <span class="code-quote">"Name"</span> property of each virtual machine seems to be already set <span class="code-keyword">for</span> me.
#       This might be because the mapping between VM Name and AD Name has been cached from adding these VMs to other desktop groups on prior occasions.
#       Keeping the statement below won't cause any problems in any <span class="code-keyword">case</span> and it should not have to be modified.
#       Just make sure the Active Directory machine names in the $adnames array are aligned to their corresponding virtual machine name in the $vmnames array.
$vms | foreach { $vm=$_; $i=-1; $vmnames | foreach { $machine=$_; $i=$i+1; <span class="code-keyword">if</span> ($machine -eq $vm.HostingName) { $vm.Name = $adnames[$i] } } }

#Add the VMs (virtual desktops) to the desktop group
$vms | foreach { $vm=$_; $xdgroup.Desktops.Add($vm); }

#Apply the change to the DDC
Set-XdDesktopGroup $xdgroup

#Verify the update
echo $xdgroup.Desktops

After executing the script, you can open the Delivery Services Console and verify that the virtual desktops have been added to the desktop group:

Analyzing the PowerShell Scripts
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.

Wrap-up
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!

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. Feel free to also check out Mike’s blog on XenApp 6 PowerShell scripting here. We hope to see you at the TechTalk!

Blogs in this series

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