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.PortStart-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.