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

Doctor Scripto

Summary: Find storage groups by using PowerShell.

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 Could you lend me a hand? I’m trying to find properties, like the current storage group that a virtual machine uses, in Azure Resource Manager, and I’m having some difficulty.

Hey, Scripting Guy! Answer Honorary Scripting Guy, Sean Kearney, is here to get you the help that you need. Storage groups? No problem.

The first time that I went to tackle this one, I was stumped. Here’s why.

I went to look at the virtual machine disks in the portal, and I saw something along the lines of this.

Screenshot of the URI of the storage group in the portal.

For the storage group, all I got was an URL! But, wasn’t the storage group named hsgstorageaccount?

In fact, it is. But as it turns out, everything you need is sitting right there. To access the storage group when you create a virtual machine by using PowerShell, what you really need is the URL to the storage account.

So, although I can copy that from the portal and take the piece that I need, it’s better to give you a programmatic solution to automatically generate the data, right? Unless you get paid by the mouse click, in which case, more power to you.

To achieve this, we need to get the properties from the OSDisk object. The storage account URL is actually a part of that.

First as always, grab the properties of the virtual machine in question:

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

The part that we’re looking for is a part of the StorageProfile object:

$VM.StorageProfile

Screenshot that shows the ImageReference and OsDisk.

Without even looking hard, we can see an object that’s named OsDisk. Let’s expand on it to see what it might have inside.

But, cue the sad trombone. There does not appear to be a storage URI in here. There’s just a reference to a VHD.

Screenshot that shows the contents of the OsDisk object.

However, when we access that property, we do see something useful inside.

$vm.StorageProfile.OsDisk.Vhd

Screenshot that shows results after accessing the Vhd property of the OsDisk object.

“Winner!”

But wait, we’re not done yet. Before we can use this URI, we need to drop the excess, which is the VHD file name.

I’m certain there is a really cool way to do what I did with regular expressions, but I found this nifty little trick with Select-String and Substring to pull it all together

First we grab the URI:

$VMStorageURL=($vm.StorageProfile.OsDisk.Vhd).uri

Next, we flush it though our little VHD cleaning trick:

$StorageGroupURL=$VMStorageURL.substring(0,(($VMStorageURL| Select-String -Pattern '/' -AllMatches).Matches[-1].Index)+1)

What this is actually doing is:

  1. To find all occurrences of the ‘/’ character, we need to know the position of the last one:

$VMStorageURL| Select-String -Pattern '/' -AllMatches).Matches[-1].Index

  1. Pull a Substring of everything from the beginning to wherever that last ‘/’ is:

$VMStorageURL.substring(0,(($VMStorageURL| Select-String -Pattern '/' -AllMatches).Matches[-1].Index)+1)

  1. And then, of course, store it back in the original object:

$StorageGroupURL=$VMStorageURL.substring(0,(($VMStorageURL| Select-String -Pattern '/' -AllMatches).Matches[-1].Index)+1)

Now, with that, you have the actual URI for the storage account that you’re going to need next week when you create a virtual machine.

One last visit tomorrow, and then the weekend. Here, we’ll try and tie up loose ends so that you can create a virtual machine and have no problems identifying the properties for PowerShell.

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

0 comments

Discussion is closed.

Feedback usabilla icon