{"id":78635,"date":"2016-06-03T00:01:23","date_gmt":"2016-06-03T07:01:23","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=78635"},"modified":"2019-02-18T09:10:41","modified_gmt":"2019-02-18T16:10:41","slug":"retrieve-azure-resource-manager-virtual-machine-properties-by-using-powershell-part-5","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/retrieve-azure-resource-manager-virtual-machine-properties-by-using-powershell-part-5\/","title":{"rendered":"Retrieve Azure Resource Manager virtual machine properties by using PowerShell \u2013 Part 5"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use PowerShell to retrieve Azure Resource Manager virtual machine properties.<\/p>\n<p>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.<\/p>\n<p>If you\u2019re new to PowerShell, you might want to first read the preceding five-part series about <a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/05\/23\/work-with-the-azurerm-cmdlets-part-1\/\">how to work with Azure Resource Manager cmdlets<\/a>.<\/p>\n<ul>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/05\/30\/retrieve-azure-resource-manager-virtual-machine-properties-by-using-powershell-part-1\/\">Retrieve Azure Resource Manager virtual machine properties by using PowerShell \u2013 Part 1<\/a><\/li>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/05\/31\/retrieve-azure-resource-manager-virtual-machine-properties-by-using-powershell-part-2\/\">Retrieve Azure Resource Manager virtual machine properties by using PowerShell \u2013 Part 2<\/a><\/li>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/06\/01\/retrieve-azure-resource-manager-virtual-machine-properties-by-using-powershell-part-3\/\">Retrieve Azure Resource Manager virtual machine properties by using PowerShell \u2013 Part 3<\/a><\/li>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/06\/02\/retrieve-azure-resource-manager-virtual-machine-properties-by-using-powershell-part-4\/\">Retrieve Azure Resource Manager virtual machine properties by using PowerShell \u2013 Part 4<\/a><\/li>\n<li>Retrieve Azure Resource Manager virtual machine properties by using PowerShell \u2013 Part 5<\/li>\n<\/ul>\n<p>After you finish this series, move on to the next series to learn more about the Azure Resource Manager cmdlets.<\/p>\n<ul>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/06\/06\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-1\/\">Create Azure Resource Manager virtual machines by using PowerShell<\/a><\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/> There\u2019s one thing I couldn\u2019t figure out that I really need your help on. Just <u>how<\/u> do you find the virtual network and network security group that a virtual machines (VM) uses?<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"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.<\/p>\n<p>When I look through the Azure portal, the answer seems obvious. I can <u>clearly<\/u> see what they are called.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-060316.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78645\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-060316.png\" alt=\"Screenshot of the Azure portal that shows the virtual network and network security group that a VM uses.\" width=\"561\" height=\"244\" \/><\/a><\/p>\n<p>But here, let\u2019s get the VM that we created before.<\/p>\n<p style=\"padding-left: 30px\"><code>$VM=Get-AzureRMVM \u2013name HSG-Linux1 \u2013ResourceGroupName HSG-AzureRG<\/code><\/p>\n<p>Now, if we look at the properties by using <strong>Get-Member<\/strong>, you\u2019ll see that there are two almost <u>dead<\/u> giveaways!<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-060316.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78655\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-060316.png\" alt=\"Screenshot that shows results of Get-Member.\" width=\"588\" height=\"51\" \/><\/a><\/p>\n<p>\u201cOkay, then!\u201d That should be easy. Just access the property of one:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-060316.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78665\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-060316.png\" alt=\"Screenshot that shows the properties of one network profile.\" width=\"602\" height=\"107\" \/><\/a><\/p>\n<p><u>That\u2019s<\/u> what I get?<\/p>\n<p>But, what you get is actually what you need. It\u2019s actually the associated ID that you can use to run against<strong>Get-AzureRMNetworkInterface<\/strong>.<\/p>\n<p>But, unfortunately, you can\u2019t just give it the string. You\u2019ll have to get all network interfaces and filter by using <strong>Where-Object<\/strong>.<\/p>\n<p style=\"padding-left: 30px\"><code>$NetworkInterfaceIDs=$VM.NetworkInterfaceIDs<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>Get-AzureRMNetworkInterface Get-AzureRmNetworkInterface | where { $_.Id -eq $vm.NetworkInterfaceIDs } | Format-Table<\/code><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4-HSG-060316.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78675\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4-HSG-060316.png\" alt=\"Screenshot that shows filtered results by using Where-Object.\" width=\"630\" height=\"60\" \/><\/a><\/p>\n<p>What we have here is the object from that particular network card on the Azure VM. This object contains <u>everything<\/u> about that card, including the network security group and the virtual network.<\/p>\n<p>Let\u2019s store that away, and then run <strong>Get-Member<\/strong> against it to see what we have to work with.<\/p>\n<p style=\"padding-left: 30px\"><code>$Nic=Get-AzureRMNetworkInterface Get-AzureRmNetworkInterface | where { $_.Id -eq $vm.NetworkInterfaceIDs }<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$Nic | Get-Member<\/code><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5-HSG-060316.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78685\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5-HSG-060316.png\" alt=\"Screenshot that shows properties of one network card of an Azure VM.\" width=\"499\" height=\"392\" \/><\/a><\/p>\n<p>\u201cAHA!\u201d You jump up. &#8220;Let me see that network security group name!\u201d<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6-HSG-060316.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78695\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6-HSG-060316.png\" alt=\"Screenshot that shows the ID of the network security group.\" width=\"582\" height=\"161\" \/><\/a><\/p>\n<p>The result is information that\u2019s similar to the network interface ID. We\u2019ll capture that for later use.<\/p>\n<p style=\"padding-left: 30px\"><code>$NSGid=$NIC.NetworkSecurityGroup.ID<\/code><\/p>\n<p>We can now filter on this against the <strong>Get-AzureRMNetworkSecurityGroup<\/strong>\u00a0cmdlet.<\/p>\n<p style=\"padding-left: 30px\"><code>$NSG=Get-AzureRmNetworkSecurityGroup | where { $_.ID -eq $NSGid }<\/code><\/p>\n<p>Now, just where is the information about our virtual network? The challenge really <u>isn\u2019t<\/u> in the VM. But, what we can pull up is the subnet information. The subnet is tucked away just underneath the <strong>IPConfigurations<\/strong> property.<\/p>\n<p style=\"padding-left: 30px\"><code>$NIC.IPConfigurations<\/code><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7-HSG-060316.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78705\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7-HSG-060316.png\" alt=\"Screenshot that shows results from the IPConfigurations property.\" width=\"549\" height=\"341\" \/><\/a><\/p>\n<p>Of course, we get that silly but powerful reference string when we pull it up.<\/p>\n<p style=\"padding-left: 30px\"><code>$VNetworkSubnetID=$NIC.IpConfigurations.subnet.id<\/code><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8-HSG-060316.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78715\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8-HSG-060316.png\" alt=\"Screenshot that shows the reference string of the virtual network.\" width=\"662\" height=\"59\" \/><\/a><\/p>\n<p>So we\u2019ll store that away for our next task, which is to identify the virtual network. For that, we use the <strong>Get-AzureRMVirtualNetwork<\/strong> cmdlet. It has a few properties that are interesting but, right now, we want to look at the subnets property.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/9-HSG-060316.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78725\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/9-HSG-060316.png\" alt=\"Screenshot that shows results of the Get-AzureRMVirtualNetwork cmdlet.\" width=\"381\" height=\"441\" \/><\/a><\/p>\n<p>Within this object is a series of subnets that have been defined for a virtual network. The data is stored as that wonderful <strong>ResourceID<\/strong>. We can filter on it in the following manner:<\/p>\n<p style=\"padding-left: 30px\"><code>$VirtualNetwork=Get-AzureRMVirtualNetwork | Where { $_.Subnets.ID \u2013match $VnetworkSubnetID }<\/code><\/p>\n<p>Congratulations! You\u2019ve 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\u2019re looking for, is to mix and match.<\/p>\n<p>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.<\/p>\n<p>How are we going to use this? That\u2019s <u>next<\/u> week when we create VMs in Azure Resource Manager here on Hey Scripting Guys!<\/p>\n<p>I invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to them at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow.<\/p>\n<p>Until then, always remember that with Great PowerShell comes Great Responsibility.<\/p>\n<p><strong>Sean Kearney\n<\/strong>Honorary Scripting Guy\nCloud and Datacenter Management MVP<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2019re new to PowerShell, you might want to first [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568,685,641],"tags":[56,154,45],"class_list":["post-78635","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-scripting-techniques","category-windows-powershell","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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\u2019re new to PowerShell, you might want to first [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=78635"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78635\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=78635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=78635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=78635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}