Retrieve Azure Resource Manager virtual machine properties by using PowerShell – Part 3


Summary: Identify properties for the operating system of a virtual machine.

This blog post is part of a series about how to retrieve Azure Resource Manager virtual machine properties 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 first read the preceding five-part series about how to work with Azure Resource Manager cmdlets.

After you finish this series, move on to the next series to learn more about the Azure Resource Manager cmdlets.

Hey, Scripting Guy! Question Can you show me how to identify the properties for the operating system so that I can duplicate a virtual machine?

Hey, Scripting Guy! Answer Honorary Scripting Guy, Sean Kearney, is here with a little more Azure and PowerShell for you. Today we’re going to examine the properties that tell us the template to use for the operating system.

Again, none of this is hidden in the portal. However, if you look, you won’t see the exact details that PowerShell will want for virtual machines.

In fact, when we look at the portal, it doesn’t even identify the version of Linux. It just says “Linux,” which really isn’t all that helpful.

Screenshot of the properties which indicate an operating system.

As we did yesterday, we’ll store the properties of the virtual machine in an object.

$VM=Get-AzureRMVM –Name HSG-Linux1 –ResourceGroupName HSG-AzureRG

The particular set of properties that we need are actually stored under a property known as StorageProfile.

$VM.StorageProfile

Buried within the StorageProfile object is a very useful property named ImageReference.

$VM.StorageProfile.ImageReference

Screenshot that shows the publisher, offer, SKU, and version information.

By using PowerShell, I can identify three of the key pieces that I will need for a virtual machine. It’s also important to note that this is common to both Linux and Windows deployments.

After I store the publisher, offer, and SKU properties, I can use this to obtain the data that I will need next week to create a virtual machine: the operating system image. We use the Get-AzureRMVMImage cmdlet for this.

To get the images, we need four pieces of Information.

  • Publisher
  • Offer
  • SKU
  • Location

We have the first three:

$Publisher=$VM.StorageProfile.ImageReference.Publisher
$Offer=$VM.StorageProfile.ImageReference.Offer
$Sku=$VM.StorageProfile.ImageReference.Sku

The location is just another available property from the virtual machine. It’s not even buried.

$Location=$VM.location

With this information, we can now access a list of virtual machine images that can be used to create a twin of this virtual machine that uses the same operating system.

Get-AzureRmVMImage -PublisherName 'Canonical' -Offer 'UbuntuServer' -Skus '14.04.4-LTS' -Location 'eastus'

Oh, how do we sort this mess out? It appears that we have too many versions to choose from. If you’d like the latest one, we can sort by the version number.

Screenshot that shows versions of Ubuntu images.

Get-AzureRmVMImage -PublisherName 'Canonical' -Offer 'UbuntuServer' -Skus '14.04.4-LTS' -Location 'eastus' | Sort-Object Version

Then, we can grab the newest one from the bottom of the pile!

(Get-AzureRmVMImage -PublisherName 'Canonical' -Offer 'UbuntuServer' -Skus '14.04.4-LTS' -Location 'eastus' | Sort-Object Version)[-1]

Pretty neat, eh?

Come on by tomorrow when we discuss how to identify the resource group and other properties that are attached to the virtual machine!

I invite you to follow the Scripting Guys on 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