Azure PowerShell, ARM templates, Visual Studio 2015 and the Azure SDK

Recently, whilst chatting to my colleague Stuart Leeks - he showed me a neat trick within PowerShell I didn't know existed.  The Out-GridView cmdlet has been around since PowerShell 2.0 and is a nice interactive UI to display PowerShell output.

I haven't had a need to use it until today when I was updating my Azure Resource Manager (ARM) template for a Visual Studio VM.  Essentially, I wanted to export a list of up to date VS Skus and then use them as allowed values within my deployment ARM template (json) file.  So let's walk through how we do this:

First up, let's get the latest list of VS Skus from the Azure data center we're going to be working with.  From within an Azure PowerShell window:

Login-AzureRmAccount

(Get-AzureRmVMImageSku -PublisherName MicrosoftVisualStudio -Offer VisualStudio -Location "West Europe") | Where-Object skus -Like 'VS-2015*'

Here, we're only interested with the West Europe region and the Visual Studio 2015 editions.  Now to get this in a nice viewable format we can use the Out-GridView cmdlet such as:

(Get-AzureRmVMImageSku -PublisherName MicrosoftVisualStudio -Offer VisualStudio -Location "West Europe") | Where-Object skus -Like 'VS-2015*' | Out-GridView

This will show the GUI which you can then add filter critiera and select which columns you want displayed:

out-gridview for skus

However, we can do more.  It's possible to take selected rows from the GUI (using the -PassThru parameter) and then pipe them into other PowerShell commands.  Here, we're passing the selected rows from the GUI to the clipboard (so we can paste them later):

(Get-AzureRmVMImageSku -PublisherName MicrosoftVisualStudio -Offer VisualStudio -Location "West Europe") | Where-Object skus -Like 'VS-2015*' | Out-GridView -PassThru | clip

So now, when we select only the VS 2015 Community Editions in the GUI, then press OK such as:

out-gridview for VS community skus

In the clipboard we now have:

clipboard 1

But what we really want is just the names of the Skus, eg. none of the other columns or headings.  So now we start becoming more selective of which columns and rows to output:

(Get-AzureRmVMImageSku -PublisherName MicrosoftVisualStudio -Offer VisualStudio -Location "West Europe") | Where-Object skus -Like 'VS-2015*' | Select-Object -ExpandProperty Skus | Out-GridView -PassThru | clip

So now, when you select only the VS Community Editions in the GUI, you get this in the clipboard:

clipboard 2

But wait, it gets better.  Within the Azure SDK 2.8.1 for .NET, json files have support when working with the clipboard.  So if you paste this text directly into a json file (in this case the ARM template) it will automatically convert each line to a string with endings and add in the comma separators :)

clipboard 3 - paste json clipboard 3 - json

Job done!