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)


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, …


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


About Sandrino Di Mattia

Sandrino Di Mattia is a Customer Success Engineer at Auth0 and a Microsoft Azure MVP.

  • Bobby Crotty

    Thanks for this script! I’m getting an error when I run it though:

  • John Bradwin

    I get same error as Bobby. Running Azure PowerShell “as Administrator.”
    Exception calling “Enable” with “4″ argument(s): “Object reference not set to an instance of an object.”
    At C:UsersDocumentsscriptingEnable-RemotePowerShellForVM.ps1:260 char:1
    + [VirtualMachineRemotePowerShell]::Enable($publishProfilePath, $subscriptionId, $ …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NullReferenceException