Not a biggie. I was bored of downloading the RDP file for my Azure VMs from the portal or going to where I downloaded them and launching from there, so I created a PowerShell function to launch RDP with my VM details directly from the command-line.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function ConnectRDP-AzureVM { [CmdletBinding()] Param( [Parameter(Position=0, Mandatory=$true)] [string]$ServiceName ) $VM = Get-AzureVM -ServiceName $ServiceName $port = (Get-AzureEndpoint -VM $VM -Name "Remote Desktop").Port $ip = (Get-AzureEndpoint -VM $VM -Name "Remote Desktop").Vip Start-Process "mstsc" -ArgumentList "/v ${ip}:$port /f" } |
Like I said, not a biggie. Add the above to your $PROFILE
and then you can connect to VMs by typing ConnectRDP-AzureVM 'vmname'
. It launches full-screen but you can change that in the function above.
Update: Use the one below. While the above snippet is fine, it uses the VM IP address and when you have many sessions open it quickly becomes confusing.
Reason I used IP address was because the Get-AzureVM
output gives the DNSName
as http://<somename>.cloudapp.net/
and I didn’t want to fiddle with extracting the name from this URL. Since I want to extract the hostname now here’s what I was going to do:
1 2 |
$VM.DNSName -match "http://(.*)/" | Out-Null $VMName = $Matches[1] |
First line does a regex match on the DNSName
attribute. Second line extracts the hostname from the match. It’s fine and does the job, but feels unelegant. Then I discovered the System.URI
class.
So now I do the following:
1 |
$VMName = ([system.uri]$VM.DNSName).Host |
Get it? I cast the URL returned by $VM.DNSName
to the System.URI
class. This gives me attributes I can work with, one of which is Host
which gives the hostname directly. Here’s the final snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function ConnectRDP-AzureVM { [CmdletBinding()] Param( [Parameter(Position=0, Mandatory=$true)] [string]$ServiceName ) $VM = Get-AzureVM -ServiceName $ServiceName $VMPort = (Get-AzureEndpoint -VM $VM -Name "Remote Desktop").Port $VMName = ([system.uri]$VM.DNSName).Host Start-Process "mstsc" -ArgumentList "/v ${VMName}:$VMPort /f" } |
Update2: Turns out there’s a cmdlet that already does the above! *smacks forehead* Didn’t find it when I searched for it but now stumbled upon it by accident. Forget my snippet, simply do:
1 |
PS> Get-AzureRemoteDesktopFile -ServiceName "service" -Name "vm" -Launch |
Replace -Launch
with -LocalPath \absolute\path\to\file.rdp
if you’d just like to save the config file instead of launching.
Update 3: In case you are using the snippet above, after deploying a VM via PowerShell I realized the Remote Desktop end point is called differently there. Eugh! Modified the code accordingly:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function ConnectRDP-AzureVM { [CmdletBinding()] Param( [Parameter(Position=0, Mandatory=$true)] [string]$ServiceName ) $VM = Get-AzureVM -ServiceName $ServiceName $VMPort = (Get-AzureEndpoint -VM $VM -Name "Remote Desktop").Port if ($VMPort -eq $null) { $VMPort = (Get-AzureEndpoint -VM $VM -Name "RemoteDesktop").Port } $VMName = ([system.uri]$VM.DNSName).Host Start-Process "mstsc" -ArgumentList "/v ${VMName}:$VMPort /f" } |