Remote desktop to XenServer VM via PowerShell

Recently I wrote a PowerShell script that connects to the console of a virtual machine on a Citrix XenServer, without using XenCenter or the Web Self Service portal. This allowed me to offer a published application to my users so they can connect to the console session. But what if they want to connect via a Remote Desktop Connection (RDP)?

I’ve written a PowerShell script that looks up the IP address of the VM and connects via RDP.

IP address

If I want my users to connect to the virtual machine via a Remote Desktop Connection (RDP) I need to know the IP address. XenCenter shows the IP address of the virtual machine when the XenServer Tools are installed, so the information is available.

Query XenServer

Before I can setup a connection for the user to the virtual via Remote Desktop Connection (RDP) I need to query the XenServer for the IP address of the virtual machine.  To query the XenServer I’m using plink (a component of the PuTTY suite) to setup a SSH connection to the specified XenServer, there’s no need to install XenCetner.

Unfortunately the xe CLI command only shows the IP adresses of all available networks.

[root@XenServer ~]# xe vm-list name-label="SERVER01" other-config:XenCenter.CustomFields.STUDENT=1 os-version:distro="windows" params=networks --minimal
0/ip: 192.168.0.50

This means that if the machine has multiple networks, the output would look like this

[root@XenServer ~]# xe vm-list name-label="SERVER01" other-config:XenCenter.CustomFields.STUDENT=1 os-version:distro="windows" params=networks --minimal
1/ip: 192.168.1.1; 0/ip: 192.168.0.50

So before I can setup the connection I need to know which network interface where looking for and extract the IP address.

$VirtualMachineNetworkID=0 (argument of the script)
$strNICInterface=($VirtualMachineNetworkID)+'/ip: '

   #Determine the IP address of the NIC can be found
   foreach ($strVMNetwork in $VMNetworks.Split(";")) {
      if ($strVMNetwork.Contains($strNICInterface)) {
	   $strVMIPaddress=$strVMNetwork.Substring($strVMNetwork.IndexOf($strNICInterface) + $strNICInterface.Length)
      }
   }

bla

Remote Desktop Connection

Setting up a connection via the Remote Desktop Connection is very easy. Just call %windir%\system32\mstsc.exe with the /v:<hostname> parameter. The /f parameter starts the session fullscreen.

%windir%\system32\mstsc.exe /v:192.168.0.50 /f

PowerShell script

I’ve written a small PowerShell script that connects to a Citrix XenServer, queries the IP address of the virtual machine and then sets up a connection via a Remote Desktop Connection (RDP). This way I don’t have to know the IP address of the virtual machine, just the name of the VM and optionally the value of a custom field.

Download : RDPXSConnect.ps1

Usage

Usage
powershell.exe .\RDPXSConnect.ps1 (XenServerPoolMaster) (XenServerUsername) (XenServerPassword) (VMName) (Network ID) [CustomFieldName] [CustomFieldValue]

Example
powershell.exe .\RDPXSConnect.ps1 172.16.1.1 root Passw0rd WS01 0 STUDENT 1

The script requires 5 arguments and has 2 optional arguments

XenServerPoolMaster: The IP/FQDN of the XenServer (pool master) host

XenServerUsername: The username to connect to the XenServer

XenServerPassword: The password to connect to the XenServer

VMName: The name of the virtual machine

Network ID : The ID of the network interface

CustomFieldName (optional): The name of a custom field

CustomFieldValue (optional): The value of the custom field

The custom field can be used to uniquely identify a virtual machine if the name of the machine is reused. In my environment I cloned the virtual machine ‘SERVER01’ multiple times and added a CustomField ‘STUDENT’. For each student a virtual machine is published.

 

Additional downloads (required)

  • plink (part of PuTTY suite)

Was once an enthusiastic PepperByte employee but is now working at Citrix. His blogs are still valuable to us and we hope to you too.