Find Azure Marketplace VM images using PowerShell

One of the cool things about running VMs in Azure is that there are many pre-built Azure Marketplace VM images you can deploy in an ARM template or with the Azure PowerShell cmdlets. But it can be difficult to find all the pieces of information one needs for an Azure Marketplace VM image, especially when you are looking for a very specific version — something that’s not “latest.”

Recently, I needed to deploy an Azure VM running Windows Server 2012 R2 with SQL Server 2014 Developer edition (don’t ask). I nearly went bonkers trying to find the right image. Largely that’s due to the poor documentation example of how to find the more abscure Azure VMs. I know I am constantly whining about the state of the Azure doc. I also know it’s a thankless task for Microsoft to keep it all up-to-date and useful. But as a daily Azure user, I feel like the documentation is always letting me down in one way or another.

The Marketplace image doc page does describe the four items of info you need to specify an image for VM deployment: publisher, offer, sku and version. But the PowerShell example is disjointed and less than useful. What’s really needed is a way to start with the top level item (publisher) and then drill down into specific offers, skus, and versions.

With PowerShell 7 bringing back the endlessly useful Out-GridView (woot!), I wanted a simple script to allow a DevOps architect to quickly locate a specific Azure VM Marketplace image. That script is posted below. You can use the output of the script to complete theimageReference property of Microsoft.Compute/virtualMachines in an ARM template that deploys a VM. Here’s an except of how you might code the output from the script into a parameters file:

..."virtualMachinePublisher": {
    "value": "MicrosoftSQLServer"
},
"virtualMachineSku":{
    "value": "sqldev"
},
"virtualMachineOffer": {
    "value": "sql2014sp3-ws2012r2"
},
"virtualMachineVersion": {
    "value": "12.21.191008"
}...

And here’s how you might use those parameters in an ARM template file:

..."imageReference": {
    "publisher": "[parameters('virtualMachinePublisher')]",
    "offer": "[parameters('virtualMachineOffer')]",
    "sku": "[parameters('virtualMachineSku')]",
    "version": "[parameters('virtualMachineVersion')]"
}...

The PowerShell script uses Out-GridView with the -PassThru parameter to allow you to select a publisher, then the offers, skus and versions you are interested in. In this video, watch how you can use Out-GridView to select multiple items to drill down into the exact Marketplace VM image you are interested. In this example, I am looking for SQL Server 2014 Enterprise, Developer and Standard images that run on Windows Server 2012 R2. The script outputs a nicely formatted table to the console and writes a .CSV file to the desktop. All you have to do is search for what you want.

Using PowerShell to find an Azure Marketplace VM image
Using PowerShell to find an Azure Marketplace VM image (click to enlarge)

This simple script shows why PowerShell 7 is so well-suited to cloud DevOps. The language has two extremely powerful constructs: object handling and the pipeline. When those two features are combined with a multi-select GUI like Out-GridView, doing even moderately complex drill-down scripts takes just a few lines of code.

Here is the script, devoid of all comments since it’s really quite simple. I hope this helps you not only find that magic Azure Marketplace VM image that will make your day go better but also helps you understand the “power of the PowerShell pipeline.” 🙂

[CmdletBinding()]
param (
    [Parameter(Mandatory=$true)]
    [string]
    $location = "eastus"
)
$pubs = Get-AzVMImagePublisher -Location $location | Select-Object PublisherName | Out-GridView -PassThru
$offers = $pubs | ForEach-Object {Get-AzVMImageOffer -Location $location -PublisherName $_.PublisherName}  | Out-GridView -PassThru
$skus = $offers | ForEach-Object {Get-AzVmImageSku -Location $location -PublisherName $_.PublisherName -Offer $_.Offer}
$versions = $skus | ForEach-Object {Get-AzVMImage -Location $location -PublisherName $_.PublisherName -Offer $_.Offer -Skus $_.Skus}
$output = $versions | Out-GridView -PassThru | Select-Object -Property Version,Skus,Offer,PublisherName,Location,Id 
$output | Export-Csv "$HOME/Desktop/AzureVmOffers.csv"
$output | Sort-Object -Property Skus | Format-Table -GroupBy Skus 

Posted

in

,

by

Comments

2 responses to “Find Azure Marketplace VM images using PowerShell”

  1. David Cobb Avatar
    David Cobb

    USEFUL! Thanks!

    1. Alex Neihaus Avatar
      Alex Neihaus

      Thanks, David!

Leave a Reply

Your email address will not be published. Required fields are marked *