Import Vmware ESX machines into a Xen Desktop “existing” catalog with a script

If you don’t feel like browsing your Vmware infrastructure everytime you add a virtual machine into Xen Desktop, you can use this script to find the virtual machine in Vmware, add it to a catalog, add it to a desktop group, add a user assignment, and set the display name of the assignment to the machine name.


Requires a catalog of the type “existing”.
Requires you to add one assignment to the machine. (You may add or remove users via the console as well)
Requires the quest AD powershell tools to verify the computer name and username
Requires that the virtual machine is named the same in VMWare as it is in Active Directory…adjust as necessary
Requires the Xen Desktop console is installed on the computer you are running the script from

#	Settings
$adminaddress = "YourXenDesktopController" #Xen Desktop Server
$DestinationCatalog = "YourDestinationCatalog"
$DestinationDesktopGroup = "YourDestinationDesktopGroup"

# Path to VDI datacenter.  This can be tricky to find. 
# I found it with the following commands:

# Add-PSSNapin -Name citrix*
# dir "XDHyp:\Connections\" -adminaddress $adminaddress

################Choose the full path of the connection displayed in the previous command
# dir "XDHyp:\Connections\YourConnectionName\" -adminaddress $adminaddress 
# Choose the full path to the required datacenter and set this to the $VDIDataCenter variable
$VDIDataCenter = "XDHyp:\Connections\yourConnectionName\YourDataCenter"

#	Check Snapins
Add-PSSnapin Quest.ActiveRoles.ADManagement
Add-PSSnapin -Name citrix* 

#	Main

	write-host "This script is intended ONLY to import virtual machines into Xen Desktop and will NOT work for physical machines"
	$VDIName = read-host "VDI Name (no domain)"
	$UserName = read-host "Username (no domain)"
	#Verify AD Account
	$VDIADObject = Get-QADComputer $VDIName -IncludedProperties samAccountName
		$ADVDIName = $VDIADObject.Domain.Name + "\" + $VDIName		
		#Verify User Account
		$ADUser = Get-QADUser $Username -IncludedProperties samAccountName
			$ADUserName = $ADUser.Domain.Name + "\" + $ADUser.samAccountName
			#Find the VDI in VMware
			#$VMWAREVDI = Get-VM $VDIName
			$VMWareHypervisor = Get-BrokerHypervisorConnection  -AdminAddress $AdminAddress
				$VDIList = dir $VDIDataCenter  -AdminAddress $AdminAddress |ForEach{ dir $_.FullPath | Where { $_.Name -eq $VDIName}} 
				ForEach($VDIObject in $VDIList){$VDIID=$VDIObject.ID}
					#Find the Catalog
					$Catalog = Get-BrokerCatalog -AdminAddress $AdminAddress | Where {$_.Name -eq $DestinationCatalog} | Select -Property UID
					$CatalogUID = $Catalog.UID
						#Import into XenDesktop Catalog
						New-BrokerMachine -MachineName $ADVDIName -CatalogUid $CatalogUid -HostedMachineId $VDIID -HypervisorConnectionUid $VMWareHypervisor.Uid -AdminAddress $AdminAddress
						#Find the Desktop Group
						$DesktopGroup = Get-BrokerDesktopGroup  -AdminAddress $AdminAddress | Where{$_.Name -eq $DestinationDesktopGroup}
							#Import into XenDesktop Group
							Add-BrokerMachine -MachineName $ADVDIName -DesktopGroup $DesktopGroup -AdminAddress $AdminAddress
							#Assign the User to the Desktop
							Add-BrokerUser -Name $ADUserName -Machine $ADVDIName
							#Set the desktop display to computername
							Set-BrokerPrivateDesktop -adminAddress $adminaddress -MachineName $ADVDIName -PublishedName $VDIName
							write-error "Unable to find Desktop Group"
						write-error "Unable to find catalog"
					write-error "Unable to find VDI in VMware"
				write-error "Unable to find hypervisor"
			write-error "Unable to find $Username in AD"
		write-error "Unable to find $VDIName in AD"

-Matthew DeBoer


2 thoughts on “Import Vmware ESX machines into a Xen Desktop “existing” catalog with a script

  1. Johannes Weidacher (toprock) October 30, 2014 / 9:20 am

    Great Script, thank you for sharing. I discovered one problem here, its because we are using Ressource Pools and they don´t get resolved in Line 53. I´m looking for a solution at the moment.

  2. mdedeboer October 30, 2014 / 11:16 am

    Hi Johannes,
    Unfortunately I don’t have access to an environment with a resource pool to test with. The environment this script is based on makes use of datacenters rather than resource pools.

    However, you should be able to decipher what is going on here….
    1. Verify that your $VDIDataCenter variable is set correctly.
    a) From an powershell prompt:

    $adminaddress = "YourXenDesktopController" #Xen Desktop Server
    Add-PSSnapin -Name citrix* 
    cd XDHyp:
    dir #You should see connections
    cd connections
    dir -adminaddress $adminaddress
    #Make note of the HypervisorConnectionName
    cd HypervisorConnectionName #Where HypervisorConnectionName is the name of your HypervisorConnection (In my case it is VMware ESX)

    This should list the available Hypervisor connections available to use. I am not sure if this will be a list of available datacenters or resource pools Make sure that the $VDIDataCenter variable is set to the FullPath property of the correct object.

    2. If you are not in the XDHyp drive, you can use this command to see the child items of your connection:

    dir $VDIDataCenter  -AdminAddress $AdminAddress 

    Here I have two clusters…which is where I am guessing you will have your resource pools. Keep following the folder structure down until you find your VDIs. If you are still in the XDHyp drive, you can use the CD and dir commands to navigate through the folder structure. I hope this helps

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s