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 MichaelWasham 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:

Parse a publish settings file to get the management certificate and the subscription ID

Find the public port for Remote PowerShell of a specific Virtual Machine

Download the certificate used by the Virtual Machine for Remote PowerShell

Install this certificate in the trusted root store

Give me some examples of how I could use Remote PowerShell for that machine

Syntax

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 !