June 3rd, 2016

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

Doctor Scripto
Scripter

Summary: Use PowerShell to retrieve Azure Resource Manager virtual machine properties.

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 There’s one thing I couldn’t figure out that I really need your help on. Just how do you find the virtual network and network security group that a virtual machines (VM) uses?

Hey, Scripting Guy! Answer Honorary Scripting Guy, Sean Kearney, is here to take away that bit of irritation. It drove me bananas, trying to figure it out.

When I look through the Azure portal, the answer seems obvious. I can clearly see what they are called.

Screenshot of the Azure portal that shows the virtual network and network security group that a VM uses.

But here, let’s get the VM that we created before.

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

Now, if we look at the properties by using Get-Member, you’ll see that there are two almost dead giveaways!

Screenshot that shows results of Get-Member.

“Okay, then!” That should be easy. Just access the property of one:

Screenshot that shows the properties of one network profile.

That’s what I get?

But, what you get is actually what you need. It’s actually the associated ID that you can use to run againstGet-AzureRMNetworkInterface.

But, unfortunately, you can’t just give it the string. You’ll have to get all network interfaces and filter by using Where-Object.

$NetworkInterfaceIDs=$VM.NetworkInterfaceIDs

Get-AzureRMNetworkInterface Get-AzureRmNetworkInterface | where { $_.Id -eq $vm.NetworkInterfaceIDs } | Format-Table

Screenshot that shows filtered results by using Where-Object.

What we have here is the object from that particular network card on the Azure VM. This object contains everything about that card, including the network security group and the virtual network.

Let’s store that away, and then run Get-Member against it to see what we have to work with.

$Nic=Get-AzureRMNetworkInterface Get-AzureRmNetworkInterface | where { $_.Id -eq $vm.NetworkInterfaceIDs }

$Nic | Get-Member

Screenshot that shows properties of one network card of an Azure VM.

“AHA!” You jump up. “Let me see that network security group name!”

Screenshot that shows the ID of the network security group.

The result is information that’s similar to the network interface ID. We’ll capture that for later use.

$NSGid=$NIC.NetworkSecurityGroup.ID

We can now filter on this against the Get-AzureRMNetworkSecurityGroup cmdlet.

$NSG=Get-AzureRmNetworkSecurityGroup | where { $_.ID -eq $NSGid }

Now, just where is the information about our virtual network? The challenge really isn’t in the VM. But, what we can pull up is the subnet information. The subnet is tucked away just underneath the IPConfigurations property.

$NIC.IPConfigurations

Screenshot that shows results from the IPConfigurations property.

Of course, we get that silly but powerful reference string when we pull it up.

$VNetworkSubnetID=$NIC.IpConfigurations.subnet.id

Screenshot that shows the reference string of the virtual network.

So we’ll store that away for our next task, which is to identify the virtual network. For that, we use the Get-AzureRMVirtualNetwork cmdlet. It has a few properties that are interesting but, right now, we want to look at the subnets property.

Screenshot that shows results of the Get-AzureRMVirtualNetwork cmdlet.

Within this object is a series of subnets that have been defined for a virtual network. The data is stored as that wonderful ResourceID. We can filter on it in the following manner:

$VirtualNetwork=Get-AzureRMVirtualNetwork | Where { $_.Subnets.ID –match $VnetworkSubnetID }

Congratulations! You’ve just found every single piece that you might need to recreate that VM! The other thing that you can do as well, since you now know what you’re looking for, is to mix and match.

You can now just spin up a VM in Azure with the configuration data that you want, shut it down, and grab the properties that you need for PowerShell.

How are we going to use this? That’s next week when we create VMs in Azure Resource Manager here on Hey Scripting Guys!

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

 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.