PowerShell, Office 365, Azure and automation
Find Azure Linux image ID or URN using PowerShell

Find Azure Linux image ID or URN using PowerShell

One day, I was playing with Chef test kitchen in Azure. There was only one option – to useĀ azurerm driver. There was heaps of examples for Ubuntu or Windows server, but I needed SUSE. It is not a big deal to provision from Azure portal, but how can I do that with Chef test kitchen… To tell the truth, my question was – where to get a link to a VM image?

I tried to copy one from a Linux SUSE VM I provisioned earlier – that wasn’t a successful attempt. Ok, I said to myself, if there is no easy way – let’s do that anyway. I found that it can be done by Get-AzVMImage PowerShell command-let, but I didn’t know what to put as requires parameters.

Get-AzVMImage
   -Location <String>
   -PublisherName <String>
   -Offer <String>
   -Skus <String>
   -Version <String>
   [-DefaultProfile <IAzureContextContainer>]
   [<CommonParameters>]

I thought – let’s just follow a way that Microsoft recommends. All the following code snippets will lead you to the image you want to use.

# Ensure that AzureRM.Computer module is loaded 
Import-Module AzureRM.Compute
# It is a requirement to login to Azure
Connect-AzureRmAccount

After connection you can run Get-AzVMImage command, but I know that the image offers are differ from region to region so you better find which Azure location you want to deploy your VM. To get a list of regions you can use this PowerShell command-let:

Get-AzureRmLocation | select Location

Output:

Get-AzureRmLocation | select Location

Let’s place the location you need to a variable, so we can use it later where needed.

$location = ''westus2

To find all the image publishers at that location you can run this command:

Get-AzureRmVMImagePublisher -Location $location

Output:

Get-AzureRmVMImagePublisher -Location $location

I was looking for a SUSE Linux, so needed to set a filter.

Get-AzureRmVMImagePublisher -Location $location | ? {$_.PublisherName -like "*suse*"} | fl

Output:

Get-AzureRmVMImagePublisher -Location $location | ? {$_.PublisherName -like "*suse*"} | fl

Now I know my publisher, so can try to find an image offer I need using Get-AzVMImageOffer command-let.

# Put publisher name to variable for easier future use
$publisher = 'SUSE'

Get-AzureRMVMImageOffer -Location $location -PublisherName $publisher

Output:

I managed to find an image offer, so I could get an SKU. Looks like too complicated, but this is a structure in Azure under the hood for images you need to follow.

# Lets set offer as a variable too
$offer = 'sles-12-sp5'

 Get-AzureRMVMImagesku -Location $location -PublisherName $publisher -Offer $offer

Output:

# Lets set offer as a variable too
$offer = 'sles-12-sp5'

 Get-AzureRMVMImagesku -Location $location -PublisherName $publisher -Offer $offer

I got what SKU I should use so I can put all the parameters to the Get-AzVMImage command-let

# set SKU as a variable
$skuName="gen1"

 Get-AzureRMVMImage -Location $location -PublisherName $publisher -Offer $offer -Skus $skuName

Output:

# set SKU as a variable
$skuName="gen1"

 Get-AzureRMVMImage -Location $location -PublisherName $publisher -Offer $offer -Skus $skuName
# Get details from a latest one
 (Get-AzureRMVMImage -Location $location -PublisherName $publisher -Offer $offer -Skus $skuName)[-1] |fl

Now we can find out what kind of images are available in Azure and can quickly get all the parameters required to choose one. In my case it was for Chef TestKitchen.

The Image URN will be $Publisher:$Offer:$Sku:$Version , in my case SUSE:sles-12-sp5:gen1:2020.06.10

These are all the parameters in one place:

$location = 'westus2'
$publisher = 'SUSE'
$offer = 'sles-12-sp5'
$skuName ='gen1'
$version = '2020.06.10'
Get-AzureRMVMImage -Location $location -PublisherName $publisher -Offer $offer -Skus $skuName -Version $version

Output:

Get-AzureRMVMImage -Location $location -PublisherName $publisher -Offer $offer -Skus $skuName -Version $version

That is a step by step guide how to find an image ID or URN you need for an OS you would like to use for a VM in Azure.

You can see all the articles about Azure or Powershell following the links.

Leave a Reply

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