Create Azure Resource Manager virtual machines by using PowerShell – Part 4


Summary: Use the Azure Resource Manager cmdlets to select the operating system image for a virtual machine.

This blog post is part of a series about how to create Azure Resource Manager virtual machines by using PowerShell. To get the most out of this series, read the posts in order.

If you’re new to PowerShell, you might want to start with the two series that precede this series:

Hey, Scripting Guy! Question I’ve got most of that virtual machine (VM) assembled, but I was having quite a time trying to pull together the operating system image with PowerShell. Nudge me in the right direction, could you?

Hey, Scripting Guy! Answer Honorary Scripting Guy, Sean Kearney, is here today to get you a little more information about how to build your own Azure VM within the Resource Manager. Our next big piece is to add in the operating system.

But, before I get much further, today is my son’s birthday. So for a few blinking moments, allow me this opportunity to wish him a Very Happy Birthday. Happy Birthday, Son!

When you worked with earlier versions of the PowerShell cmdlets for Azure, you would just run the Get-AzureVMImage cmdlet. This one cmdlet would list every single image that’s available in the gallery.

The problem here is that with 979 images at last count, it would be a massive pause to get them all. Furthermore, consider that you might only want the Microsoft or Linux images? You still had to pull down this large list and then filter it.

When we created out first Linus VM, we pulled out the properties of the VM in the following manner:

$VM=Get-AzureRMVMImage –name HSG-Linux1 –resourcegroupname HSG-AzureRG

$Publisher=$VM.StorageProfile.ImageReference.Publisher

$Offer=$VM.StorageProfile.ImageReference.Offer

$Sku=$VM.StorageProfile.ImageReference.Sku

$Version=$VM.StorageProfile.ImageReference.Version

These are far more manageable collections because the images themselves are now categorized. These four categories are:

  • PublisherName
  • Offer
  • Sku
  • Version

PublisherName

This property contains a descriptive name for the vendor that holds images. However, it’s not as obvious as “Microsoft” or “Linux” in many cases. This is where I find creating a VM first helps. It contains all these properties that I can filter out.

You can obtain the complete list by using Get-AzureRMVMImagePublisher. Because the list of images is different among locations, you’ll need to provide a location.

Get-AzureRMVMImagePublisher –location ‘eastus’

In the case of our Linux VM, the publisher is ‘Canonical’. Yes, I don’t quite get that one either, That’s why I personally find creating a VM first and pulling its properties far easier.

Offer

In most cases, this will typically be the base name of the operating system, such as “Ubuntu Server” or “Windows Server”. I can, for example, use an additional cmdlet if I supply the PublisherName to list all available offers. That cmdlet is Get-AzureRMVMImageOffer, and it requires both a publisher and location.

Get-AzureRMVMImageOffer –location ‘eastus’ –PublisherName ‘Canonical’

From our last VM, the actual offer name was ‘UbuntuServer’.

Sku

When you target a particular PublisherName and Offer, you’ll next target a SKU. Think of the SKU as a refinement of the list, such as Server 2012 R2, Datacenter Edition, or Standard Edition.

To get a list of available SKUs, you’ll need to supply the PublisherName, Offer, and Location to the Get-AzureRMVMImageSKU cmdlet.

Get-AzureRMVMImageSKU –location ‘eastus’ –PublisherName ‘Canonical’ –offer ‘UbuntuServer’

What you’ll notice each time is that the list is smaller and quicker to pull down. Also, as long as you know the particular breakdown, it’s easy to switch a VM script by just supplying these first three pieces. It’s also much faster now to pull down the list of images.

Version

This last property is closer to what you might think of as the patch level. To identify available versions, you need to supply all the first three properties to the Get-AzureRMVMImage cmdlet. This will give you an identified list of images from which you’ll need to select the version.

Here’s where the VM won’t help you much. When you pull its version property, you’ll often see this value, Latest.

But if you try to target that against a “Where-object”, you’ll never get it. If a version is not supplied, we can sort the images against the Version object. In the following example, we pull all the available images and target the “Latest” or newest in the following manner.

$Version=(Get-AzureRMVMImage –location ‘eastus’ –PublisherName ‘Canonical’ –Offer ‘UbuntuServer’ –skus ’14.04.4-LTS’ | Sort-object Version)[-1].Version

After we have all of this information, the trick is to add it to a VM image. This is done through the use of the Set-AzureRMVMSoureImage cmdlet. If you have all of these four properties, you can supply them in the following manner:

In the following example, we add them to our existing VM image object:

$Publisher=’Canonical’

$Offer=’UbuntuServer’

$Sku=’ 14.04.4-LTS’

$Version=’ 14.04.201605160’

$AzureVM = Set-AzureRmVMSourceImage -VM $AzureVM -PublisherName $Publisher -Offer $Offer -Skus $Skus -Version $VMImage.Version

Using the same process from last week, I spun up a Server 2016 image from MSDN. Here is an example of the same image properties from a Microsoft environment:

$Publisher=’MicrosoftWindowsServer’

$Offer=’WindowsServer’

$Sku=’ Windows-Server-Technical-Preview’

$Version=’ 5.0.20160420’

$AzureVM = Set-AzureRmVMSourceImage -VM $AzureVM -PublisherName $Publisher -Offer $Offer -Skus $Sku -Version $VMImage.Version

At this point, there is only one small remaining piece. For that, return tomorrow.

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow.

Until then, always remember that with Great PowerShell comes Great Responsibility.

Sean Kearney
Honorary Scripting Guy
Cloud and Datacenter Management MVP

Comments (0)

Skip to main content