Opening RDP session to an Azure VM with PowerShell
UPDATE (4 March 2015): The method described in this post is now obsolete as a new PowerShell cmdlet Get-AzureRemoteDesktopFile provides this functionality! Check out the documentation at https://msdn.microsoft.com/en-us/library/azure/dn495261.aspx
One of the things I have found myself doing a lot lately is tearing down and rebuilding environments in Windows Azure (using the virtual machines functionality, which is very cool). One of the problems I came across with this approach was that when I would manually create a VM that I planned to keep there I would be able to specify the RDP port to use for remote communication, but the scripts I'm using to prepare several machines at once don't give me that luxury (although I could probably script updating the end points, I haven't done that yet). So what I needed was a way to quickly look up the end point and open a remote desktop connection to the VM based on me just knowing the machine name and cloud service name - lucky for me this was very simple:
$vm = Get-AzureVM -ServiceName $cloudService -Name $vmName
$rdp = Get-AzureEndpoint -Name "RDP" -VM $vm
$hostdns = (New-Object "System.Uri" $vm.DNSName).Authority
$port = $rdp.Port
Start-Process "mstsc" -ArgumentList "/V:$hostdns`:$port /w:1024 /h:768"
This is a pretty straight forward bit of script that uses the Windows Azure PowerShell cmdlets - basically we use the Get-AzureVM command to identify the VM we want to connect to (substitute in your own cloud service and VM name's here), and then we move on to the Get-AzureEndpoint to look up the RDP endpoint details. From this we can use the properties of the VM (through the DNSName property) and the RDP details for port number to kick off a command line call to MSTSC, the remote desktop application. You can throw whatever parameters on the end of the MSTSC call that you need, in my case I wanted it to be a windowed view at 1024x768. That's it - run the script and your remote session will open without needing to know the port the VM uses RDP on ahead of time.
Comments
- Anonymous
May 29, 2014
not sure when this changed. but the Get-AzureEndpoint service name is now "Remote Desktop" not "RDP" - Anonymous
May 29, 2014
Hi Bill,It actually varies a little. Any of the Azure VM's I create through the browser UI get "Remote Desktop" as you described, but when I create VMs through the PowerShell modules they come out as "RDP" like my sample above. So it will vary from scenario to scenario.Hope that helps! :) - Anonymous
January 29, 2015
You could instead find the endpoint with the LocalPort value of 3389. That would solve the problem with the name being "RDP" sometimes and "Remote Desktop" others:$rdp = (Get-AzureEndpoint -VM $vm | where { $_.LocalPort -eq 3389}) - Anonymous
January 29, 2015
Great point Bret - that is a much better approach! - Anonymous
March 03, 2015
Could now use Get-AzureRemoteDesktopFile instead. msdn.microsoft.com/.../dn495261.aspx - Anonymous
March 03, 2015
Thanks for the link Ryan! I've updated the post with a link to this new cmdlet!