{"id":78575,"date":"2016-06-02T00:01:03","date_gmt":"2016-06-02T07:01:03","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=78575"},"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-4","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/retrieve-azure-resource-manager-virtual-machine-properties-by-using-powershell-part-4\/","title":{"rendered":"Retrieve Azure Resource Manager virtual machine properties by using PowerShell \u2013 Part 4"},"content":{"rendered":"<p><strong>Summary:<\/strong>\u00a0Find storage groups by using PowerShell.<\/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>Retrieve Azure Resource Manager virtual machine properties by using PowerShell \u2013 Part 4<\/li>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/06\/03\/retrieve-azure-resource-manager-virtual-machine-properties-by-using-powershell-part-5\/\">Retrieve Azure Resource Manager virtual machine properties by using PowerShell \u2013 Part 5<\/a><\/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\" \/> Could you lend me a hand? I\u2019m trying to find properties, like the current storage group that a virtual machine uses, in Azure Resource Manager, and I\u2019m having some difficulty.<\/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 get you the help that you need. Storage groups? No problem.<\/p>\n<p>The first time that I went to tackle this one, I was stumped. Here\u2019s why.<\/p>\n<p>I went to look at the virtual machine disks in the portal, and I saw something along the lines of this.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-060216.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78585\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-060216.png\" alt=\"Screenshot of the URI of the storage group in the portal.\" width=\"514\" height=\"179\" \/><\/a><\/p>\n<p>For the storage group, all I got was an URL! But, wasn\u2019t the storage group named <em>hsgstorageaccount<\/em>?<\/p>\n<p>In fact, it <u>is<\/u>. But as it turns out, everything you need is sitting right there. To access the storage group\u00a0when you create a virtual machine by using PowerShell, what you really need is the URL to the storage account.<\/p>\n<p>So, although I <u>can<\/u> copy that from the portal and take the piece that I need, it\u2019s 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.<\/p>\n<p>To achieve this, we need to get the properties from the <strong>OSDisk<\/strong> object. The storage account URL is actually a part of that.<\/p>\n<p>First as always, grab the properties of the virtual machine in question:<\/p>\n<p style=\"padding-left: 30px\"><code>$VM=Get-AzureRMVM \u2013Name HSG-Linux1 \u2013ResourceGroupName HSG-AzureRG<\/code><\/p>\n<p>The part that we\u2019re looking for is a part of the <strong>StorageProfile<\/strong> object:<\/p>\n<p style=\"padding-left: 30px\"><code>$VM.StorageProfile<\/code><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-060216.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78595\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-060216.png\" alt=\"Screenshot that shows the ImageReference and OsDisk.\" width=\"718\" height=\"46\" \/><\/a><\/p>\n<p>Without even looking hard, we can see an object that\u2019s named <strong>OsDisk<\/strong>. Let\u2019s expand on it to see what it might have inside.<\/p>\n<p>But, cue the sad trombone. There does not appear to be a storage URI in here. There\u2019s just a reference to a VHD.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-060216.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78605\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-060216.png\" alt=\"Screenshot that shows the contents of the OsDisk object.\" width=\"679\" height=\"126\" \/><\/a><\/p>\n<p>However, when\u00a0we access that property, we <u>do<\/u> see something useful inside.<\/p>\n<p style=\"padding-left: 30px\"><code>$vm.StorageProfile.OsDisk.Vhd<\/code><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4-HSG-060216.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78615\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4-HSG-060216.png\" alt=\"Screenshot that shows results after accessing the Vhd property of the OsDisk object.\" width=\"674\" height=\"60\" \/><\/a><\/p>\n<p>\u201cWinner!\u201d<\/p>\n<p>But wait, we\u2019re not done yet. Before we can use this URI, we need to drop the excess, which is the VHD file name.<\/p>\n<p>I\u2019m certain there is a really cool way to do what I did with regular expressions, but I found this nifty little trick with <strong>Select-String<\/strong> and <strong>Substring<\/strong> to pull it all together<\/p>\n<p>First we grab the URI:<\/p>\n<p style=\"padding-left: 30px\"><code>$VMStorageURL=($vm.StorageProfile.OsDisk.Vhd).uri<\/code><\/p>\n<p>Next, we flush it though our little VHD cleaning trick:<\/p>\n<p style=\"padding-left: 30px\"><code>$StorageGroupURL=$VMStorageURL.substring(0,(($VMStorageURL| Select-String -Pattern '\/' -AllMatches).Matches[-1].Index)+1)<\/code><\/p>\n<p>What this is actually doing is:<\/p>\n<ol>\n<li>To find all occurrences of the \u2018\/\u2019 character, we need to know the position of the last one:<\/li>\n<\/ol>\n<p style=\"padding-left: 60px\"><code>$VMStorageURL| Select-String -Pattern '\/' -AllMatches).Matches[-1].Index<\/code><\/p>\n<ol start=\"2\">\n<li>Pull a <strong>Substring<\/strong> of everything from the <u>beginning<\/u> to wherever that last \u2018\/\u2019 is:<\/li>\n<\/ol>\n<p style=\"padding-left: 60px\"><code>$VMStorageURL.substring(0,(($VMStorageURL| Select-String -Pattern '\/' -AllMatches).Matches[-1].Index)+1)<\/code><\/p>\n<ol start=\"3\">\n<li>And then, of course, store it <u>back<\/u> in the original object:<\/li>\n<\/ol>\n<p style=\"padding-left: 60px\"><code>$StorageGroupURL=$VMStorageURL.substring(0,(($VMStorageURL| Select-String -Pattern '\/' -AllMatches).Matches[-1].Index)+1)<\/code><\/p>\n<p>Now, with <u>that<\/u>, you have the actual URI for the storage account that you\u2019re going to need next week when you create a virtual machine.<\/p>\n<p>One last visit tomorrow, and then the weekend. Here, we\u2019ll try and tie up loose ends so that you can create a virtual machine and have no problems identifying the properties for PowerShell.<\/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<\/strong>\nHonorary Scripting Guy\nCloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary:\u00a0Find 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\u2019re new to PowerShell, you might want to first read the preceding five-part series [&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,641],"tags":[56,154,45],"class_list":["post-78575","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-windows-powershell","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary:\u00a0Find 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\u2019re new to PowerShell, you might want to first read the preceding five-part series [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78575","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=78575"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78575\/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=78575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=78575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=78575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}