Today I want to provide a PowerShell script that can be used to export information from a XenDesktop Hyper-V installation and import it back into the Provisioning Services (PVS) console. This script is useful when you are not using the XenDesktop Setup Wizard and are creating the virtual machines manually. Exporting and importing is much quicker than manually adding each guest VM machine name and MAC address to the console.

The PowerShell script should be run from the SCVMM server that manages the XenDesktops that you are importing into Provisioning Server.

Syntax Information

Usage: GenPVSFile.ps1 SiteName CollectionName Description ImportFileName VMMatchCriteria
Example: .\GenPVSFile.ps1 “Dell” “XenDesktops” “XD Desktop” “c:\PVSImport.csv” HVDesktop

The example will locate all VMs with a name that matches HVDesktop (HVDesktop1, 23HVDesktop, etc.) and generate a CSV file called PVSImport.csv. When imported via the PVS console, the devices will go into the XenDesktops collection for the Dell site.

GenPVSFile PowerShell Script
# Purpose:       Create a CSV file that can be imported by Provisioning services
# Date:          14 April 2010
# Author:        Paul Wilson
# Notes:         The CSV file may need to opened and saved in the ANSI format on some computers.
#                This script automatically appends information to any existing file in <span class="code-keyword">case</span> you need to
#                run the command multiple times with different match criteria.

<span class="code-keyword">if</span> ($args -eq $<span class="code-keyword">null</span> -or $args.Count -lt 5)
{
    write-output <span class="code-quote">"Usage: GenPVSFile.ps1 SiteName CollectionName Description ImportFileName VMMatchCriteria"</span>
    write-output <span class="code-quote">"Example: .\GenPVSFile.ps1 "</span><span class="code-quote">"Site"</span><span class="code-quote">" "</span><span class="code-quote">"Collection"</span><span class="code-quote">" "</span><span class="code-quote">"XD Desktop"</span><span class="code-quote">" "</span><span class="code-quote">"c:\PVSImport.csv"</span><span class="code-quote">" HVDesktop "</span>
    exit 1
}
# Pulls the VM Name Match criteria off the command-line
$VMNameMatches = $args[4]

# Connects to the local SCVMM Server
$VMMServer = Get-VMMServer -Computername <span class="code-quote">"localhost"</span>

# Finds all matching VMs and sorts by their machine name
$AllVMs = Get-VM | where { $_.Name -match <span class="code-quote">"$VMNameMatches"</span> } | sort Name

# The following loop gets the MAC address of the primary NIC then writes
# that output to the CSV file along with the other fields required <span class="code-keyword">for</span> the PVS <span class="code-keyword">import</span>
# most of which were supplied as parameters on the command-line.

foreach ($vm in $AllVms)
{
  $nicDetails = Get-VirtualNetworkAdapter -VM $vm

  $csvString = <span class="code-quote">"{0},{1},{2},{3},{4}"</span> -f $vm.Name, $nicDetails[0].PhysicalAddress, $args[0], $args[1], $args[2]
  write $csvString | out-File $args[3] -Append
}

After you have the file, you have two options for importing the data into Provisioning Server. Before doing either, the best approach is to configure a default template for the collection and set the appropriate vDisk for that template. This way you will not later need to go back and assign a vDisk to all the devices. You can use the PVS Administrator’s guide to obtain instructions on how to create and assign a device template.

Using the GUI you can select a device collection, then right-click and choose Target Device >> Import Devices… from the context menu as shown in the screen shot below.

Alternatively you can use the MCLI command-line utility that comes with Provisioning Server. The MCLI commands must be run from a Provisioning server at this point. You can find MCLI in the %ProgramFiles%\Citrix\Provisioning Services folder. The command-line for importing looks like this:

MCLI run importdevices -p filename=\\server\share\pvsimport.csv copyTemplate=1

Remember after you finish importing all the MAC addresses be sure to add them to Active Directory. You can do this by selecting all of the devices, right-clicking on them and choosing Active Directory >> Create Machine Account… from the context menu as shown below.

If you found this information useful and would like to be notified of future blog posts, please follow me on Twitter @pwilson98 or visit my XenDesktop on Microsoft website.