Retrieve Azure Resource Manager virtual machine properties by using PowerShell – Part 2
Summary: Use the Azure Resource Manager cmdlets to get the machine size from 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.
- Retrieve Azure Resource Manager virtual machine properties by using PowerShell – Part 1
- Retrieve Azure Resource Manager virtual machine properties by using PowerShell – Part 2
- Retrieve Azure Resource Manager virtual machine properties by using PowerShell – Part 3
- Retrieve Azure Resource Manager virtual machine properties by using PowerShell – Part 4
- Retrieve Azure Resource Manager virtual machine properties by using PowerShell – Part 5
After you finish this series, move on to the next series to learn more about the Azure Resource Manager cmdlets.
I remember that yesterday we created a virtual machine (VM), and you mentioned we could find out things like the size of the machine that we chose. Could you show me how to do that by using PowerShell?
Honorary Scripting Guy, Sean Kearney, is here today to show you something neat: how to get the configuration data from a VM. Today, we’re going to target the machine size.
We could just go to the Azure portal right now and find this information, of course. If we click the VM and look in the right pane, we’ll see the size right there: Standard A0.
But, in no way does this guarantee that the property will be exactly what PowerShell wants when it creates a VM.
To do this, we need to get the properties of the VM by using the Azure Resource Manager cmdlets. For this, we use the Get-AzureRMVM cmdlet. So, let’s target the Linux machine that we created yesterday.
Get-AzureRMVM –Name HSG-Linux1 –ResourceGroupName HSG-AzureRG
Now, it would be much better if we captured that information as an Object to make it easier and quicker to ask it questions as opposed to running and asking Azure Resource Manager questions every few minutes.
$VM= Get-AzureRMVM –Name HSG-Linux1 –ResourceGroupName HSG-AzureRG
Now that we have a copy to play with, let’s run this through Get-Member to see what information is available.
$VM | Get-Member
We’ve got a whole pile of useful information here. Most of the properties are pretty well organized in Azure Resource Manager. In the case of the size of the VM, it’s actually stored under the HardwareProfile property. Let’s access that right now
As we can see, the actual name that PowerShell will need has an underscore within it, so a visual check of the screen wouldn’t have helped.
To store the hardware profile for future reference, we can pull it out by using Select-Object.
$VM.HardwareProfile | Select-Object -ExpandProperty VMSize
As an alternative, we could have even just accessed that individual property in the following manner:
With this information, we can pipe to the CLIP command (not Clippy’s cousin) so that we can put it into a new script to avoid typing, of course.
$VM.HardwareProfile.VmSize | CLIP
Now that it’s on the clipboard, you can CTRL-V it to wherever you need.
Step on over for a visit tomorrow when we show you how to identify the operating system template in use for our VM.
I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow.
Until then always remember that with Great PowerShell comes Great Responsibility.
Honorary Scripting Guy
Cloud and Datacenter Management MVP