Script to automatically configuring Remote PowerShell for Windows Azure Virtual Machines on your machine

In my last post I explained how to leverage the support for Remote PowerShell in your Windows Azure Virtual Machines. While this is fairly easy to setup, it can get a little boring if you need to repeat this over and over again (especially the part with the certificates). A few weeks ago Michael Washam explained how to automate this with the Windows Azure PowerShell SDK: Windows Azure PowerShell Updates for IaaS GA.

From time to time I tend to suffer from the “not invented here” syndrome, so I decided to write a little script which would do the following:

  1. Parse a publish settings file to get the management certificate and the subscription ID
  2. Find the public port for Remote PowerShell of a specific Virtual Machine
  3. Download the certificate used by the Virtual Machine for Remote PowerShell
  4. Install this certificate in the trusted root store
  5. Give me some examples of how I could use Remote PowerShell for that machine

The result: Enable-RemotePowerShellForVM.ps1 (download)

Syntax

You can call the script like this:

Enable-RemotePowerShellForVM.ps1 “X:\mycredentials.publishsettings” “Name Of My Subscription” “Name of my Cloud Service” “Name of my VM

Now this means that, after installing your Virtual Machine with Remote PowerShell enabled, you only need to run one line of code on  your machine to setup Remote PowerShell on your local machine. After that you can start doing things like deploy an application, install SharePoint, a TFS Build Server, …

Source

One of the reasons why I created this script was to test out a nice PowerShell feature that allows you to create types (which you can use in PowerShell) by using C# code. I actually wrote this script in a Console Application and then just copied the code to a .ps1 file. Finally, by calling Add-Type we turn the C# code into a type which can then be used in PowerShell. Call me lazy, but it’s so much easier to do C# in PowerShell :)

Enjoy!

About Sandrino Di Mattia

Sandrino Di Mattia is a Windows Azure Consultant at RealDolmen and a Windows Azure Insider. He lives and breathes Windows Azure.