Remote desktop to XenServer VM via PowerShell

Written by Ingmar Verheij on February 14th, 2012. Posted in PowerShell, XenServer

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.

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

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

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.

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

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)

Ingmar Verheij

At the time Ingmar wrote this article he worked for PepperByte as a Senior Consultant (up to May 2014). His work consisted of designing, migrating and troubleshooting Microsoft and Citrix infrastructures. He was working with technologies like Microsoft RDS, user environment management and (performance) monitoring. Ingmar is User Group leader of the Dutch Citrix User Group (DuCUG). RES Software named Ingmar RES Software Valued Professional in 2014.

More Posts - Website

Follow Me:
TwitterLinkedInGoogle Plus

Tags: , , ,

Leave a comment

*

Donate

%d bloggers like this: