Simple Azure VM RDP PowerShell Script

Background

If you’re using the Microsoft Azure platform, you probably have built up one or more virtual machines. Virtual machines in Azure are great for testing, because you can easily destroy and rebuild them if you mess something up. As an additional benefit, you don’t have to risk messing up your local desktop, laptop, or tablet, with configurations that could result in destroying your own productivity!

Connecting to an Azure Virtual Machine using Remote Desktop Protocol (RDP) is easy from the Azure Management Portal, or from the Azure PowerShell module. Because I use PowerShell on such a regular basis, and because I generally dislike pointing and clicking in GUIs, I often use the Get-AzureRemoteDesktopFile command to invoke a RDP session to my Azure VMs. The unfortunate part is that there’s a lot of typing involved with calling this command.

  1. First, you have to make sure that you have “selected” the correct Azure subscription, using the Select-AzureSubscription command. Many people have access to multiple subscriptions, and manage resources across them all.
  2. Secondly, you have to specify three parameters for the call to Get-AzureRemoteDesktopFile: the Cloud Service name, the Virtual Machine name, and then the -Launch parameter to immediately invoke the RDP file.

Automate All The Things!

To simplify this process, I’ve written a small wrapper function, that allows you to select an Azure subscription, and then a virtual machine, using a simple GUI. The Out-GridView command allows you to select one or multiple objects, and pass them down the PowerShell pipeline. I’ve used that to my advantage, rather than writing a custom GUI. To navigate the Out-GridView window, you don’t even have to move over to the mouse. You can simply use the {Up/Down Arrow}, and the {ENTER} key to select which item you want to work with. This helps streamline your work efforts, because:

  1. You don’t have to remember the exact Azure subscription name, Cloud Service name or Virtual Machine name
  2. You don’t have to type out the full commands to select your Azure subscription and invoke RDP against your Cloud Service + Virtual Machine.

Usage

Select your Azure Subscription using Out-GridView

Select your Azure Subscription using Out-GridView

To use this azrdp function, follow these steps:

  1. Make sure that you install the latest Azure PowerShell module.
  2. Copy / paste the function into your PowerShell profile script.
  3. Start a new PowerShell session, and type the following: azrdp -P{TAB}{ENTER}.
  4. Use the arrow keys to select your subscription, and press {ENTER}.
  5. The script will prompt you to select a virtual machine. Use the arrow keys and press {ENTER}.
  6. The script will download and invoke a RDP file, and you can log into your Azure Virtual Machine!

NOTE: If you need to search inside the Out-GridView dialog box, you can use the {CTRL} + E keyboard shortcut, and start typing the partial text of any unique content in the window!

That’s all there is to it! Feel free to follow on Twitter @pcgeek86, and check back on my website for future articles!