{"id":3322,"date":"2012-02-14T14:14:34","date_gmt":"2012-02-14T12:14:34","guid":{"rendered":"https:\/\/ingmarverheij.com\/?p=3322"},"modified":"2013-11-30T13:07:20","modified_gmt":"2013-11-30T12:07:20","slug":"remote-desktop-to-xenserver-vm-via-powershell","status":"publish","type":"post","link":"https:\/\/ingmarverheij.com\/en\/remote-desktop-to-xenserver-vm-via-powershell\/","title":{"rendered":"Remote desktop to XenServer VM via PowerShell"},"content":{"rendered":"<p>Recently I wrote a <a href=\"https:\/\/ingmarverheij.com\/2012\/02\/connect-to-vm-without-xencenter\/\" target=\"_blank\">PowerShell script<\/a> 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)?<\/p>\n<p>I\u2019ve written a PowerShell script that looks up the IP address of the VM and connects via RDP.<\/p>\n<p><a href=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2012\/02\/Web-Interface.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; margin: 0px 5px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"\" alt=\"\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2012\/02\/Web-Interface_thumb.png\" width=\"554\" height=\"145\" border=\"0\" \/><\/a><\/p>\n<p><!--more--><\/p>\n<h4>IP address<\/h4>\n<p>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.<\/p>\n<p><a href=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2012\/02\/XenCenter-Server01.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; margin: 0px 5px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"\" alt=\"\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2012\/02\/XenCenter-Server01_thumb.png\" width=\"554\" height=\"136\" border=\"0\" \/><\/a><\/p>\n<h4><\/h4>\n<h4>Query XenServer<\/h4>\n<p>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.\u00a0 To query the XenServer I\u2019m using plink (a component of the <a href=\"https:\/\/www.chiark.greenend.org.uk\/~sgtatham\/putty\/download.html\" target=\"_blank\">PuTTY<\/a> suite) to setup a SSH connection to the specified XenServer, there\u2019s no need to install XenCetner.<\/p>\n<p>Unfortunately the <a href=\"https:\/\/docs.vmd.citrix.com\/XenServer\/6.0.0\/1.0\/en_gb\/reference.html#id899059\" target=\"_blank\">xe CLI command<\/a> only shows the IP adresses of all available networks.<\/p>\n<pre lang=\"bash\">[root@XenServer ~]# xe vm-list name-label=\"SERVER01\" other-config:XenCenter.CustomFields.STUDENT=1 os-version:distro=\"windows\" params=networks --minimal\r\n0\/ip: 192.168.0.50<\/pre>\n<p>This means that if the machine has multiple networks, the output would look like this<\/p>\n<pre lang=\"bash\">[root@XenServer ~]# xe vm-list name-label=\"SERVER01\" other-config:XenCenter.CustomFields.STUDENT=1 os-version:distro=\"windows\" params=networks --minimal\r\n1\/ip: 192.168.1.1; 0\/ip: 192.168.0.50<\/pre>\n<p>So before I can setup the connection I need to know which network interface where looking for and extract the IP address.<\/p>\n<pre lang=\"powershell\">$VirtualMachineNetworkID=0 (argument of the script)\r\n$strNICInterface=($VirtualMachineNetworkID)+'\/ip: '\r\n\r\n   #Determine the IP address of the NIC can be found\r\n   foreach ($strVMNetwork in $VMNetworks.Split(\";\")) {\r\n      if ($strVMNetwork.Contains($strNICInterface)) {\r\n\t   $strVMIPaddress=$strVMNetwork.Substring($strVMNetwork.IndexOf($strNICInterface) + $strNICInterface.Length)\r\n      }\r\n   }<\/pre>\n<p>bla<\/p>\n<h4>Remote Desktop Connection<\/h4>\n<p>Setting up a connection via the Remote Desktop Connection is very easy. Just call<em> %windir%\\system32\\mstsc.exe<\/em> with the <em>\/v:&lt;hostname&gt;<\/em> parameter. The <em>\/f<\/em> parameter starts the session fullscreen.<\/p>\n<pre>%windir%\\system32\\mstsc.exe \/v:192.168.0.50 \/f<\/pre>\n<h4 id=\"download\">PowerShell script<\/h4>\n<p>I\u2019ve 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\u2019t have to know the IP address of the virtual machine, just the name of the VM and optionally the value of a custom field.<\/p>\n<p>Download:\u00a0<a  data-e-Disable-Page-Transition=\"true\" class=\"download-link\" title=\"Version 1.0\" href=\"https:\/\/ingmarverheij.com\/en\/download\/5815\/?tmstv=1776621952\" rel=\"nofollow\" id=\"download-link-5815\" data-redirect=\"false\" >\n\tRDPXSConnect.ps1<\/a>\n<\/p>\n<h4><\/h4>\n<h4>Usage<\/h4>\n<pre><strong>Usage<\/strong>\r\npowershell.exe .\\RDPXSConnect.ps1 (XenServerPoolMaster) (XenServerUsername) (XenServerPassword) (VMName) (Network ID) [CustomFieldName] [CustomFieldValue]\r\n\r\n<strong>Example<\/strong>\r\npowershell.exe .\\RDPXSConnect.ps1 172.16.1.1 root Passw0rd WS01 0 STUDENT 1<\/pre>\n<p>The script requires 5 arguments and has 2 optional arguments<\/p>\n<p>XenServerPoolMaster: The IP\/FQDN of the XenServer (pool master) host<\/p>\n<p>XenServerUsername: The username to connect to the XenServer<\/p>\n<p>XenServerPassword: The password to connect to the XenServer<\/p>\n<p>VMName: The name of the virtual machine<\/p>\n<p>Network ID : The ID of the network interface<\/p>\n<p>CustomFieldName (optional): The name of a custom field<\/p>\n<p>CustomFieldValue (optional): The value of the custom field<\/p>\n<p>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 \u2018SERVER01\u2019 multiple times and added a CustomField \u2018STUDENT\u2019. For each student a virtual machine is published.<\/p>\n<p><a href=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2012\/02\/WS01-Properties_2012-02-02_11-38-161.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; margin: 0px 5px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"\" alt=\"\" src=\"https:\/\/ingmarverheij.com\/wp-content\/uploads\/2012\/02\/WS01-Properties_2012-02-02_11-38-16_thumb1.png\" width=\"554\" height=\"374\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h4>Additional downloads (required)<\/h4>\n<ul>\n<li><a href=\"https:\/\/www.chiark.greenend.org.uk\/~sgtatham\/putty\/download.html\">plink<\/a> (part of PuTTY suite)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[152,77],"tags":[667,672,196,669],"class_list":["post-3322","post","type-post","status-publish","format-standard","hentry","category-powershell","category-xenserver","tag-citrix","tag-powershell","tag-rdp","tag-xenserver"],"_links":{"self":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts\/3322","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/comments?post=3322"}],"version-history":[{"count":4,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts\/3322\/revisions"}],"predecessor-version":[{"id":5892,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts\/3322\/revisions\/5892"}],"wp:attachment":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/media?parent=3322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/categories?post=3322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/tags?post=3322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}