Summary: Use PowerShell to retrieve Azure Resource Manager virtual machine properties.
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?
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.
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!
“Okay, then!” That should be easy. Just access the property of one:
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
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
“AHA!” You jump up. “Let me see that network security group name!”
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
Of course, we get that silly but powerful reference string when we pull it up.
$VNetworkSubnetID=$NIC.IpConfigurations.subnet.id
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.
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