{"id":78745,"date":"2016-06-07T00:01:05","date_gmt":"2016-06-07T07:01:05","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=78745"},"modified":"2019-02-18T09:10:40","modified_gmt":"2019-02-18T16:10:40","slug":"create-azure-resource-manager-virtual-machines-by-using-powershell-part-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-2\/","title":{"rendered":"Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 2"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use the Azure Resource Manager cmdlets to define the operating system disk on a storage blob for a virtual machine.<\/p>\n<p>This blog post is part of a series about how to create Azure Resource Manager virtual machines by using PowerShell. To get the most out of this series, read the posts in order.<\/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 \u2013 Part 1<\/a><\/li>\n<li>Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 2<\/li>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/06\/08\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-3\/\">Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 3<\/a><\/li>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/06\/09\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-4\/\">Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 4<\/a><\/li>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/06\/10\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-5\/\">Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 5<\/a><\/li>\n<\/ul>\n<p><span style=\"color: #008000\">If you\u2019re new to PowerShell, you might want to start with the two series that precede this series:<\/span><\/p>\n<ul>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/05\/23\/work-with-the-azurerm-cmdlets-part-1\/\">Work with the Azure Resource Manager cmdlets<\/a><\/li>\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<\/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\" \/> Would you help me, please?\u00a0 I need to know how to define storage for my virtual machine in Azure Resource Manager by using PowerShell.<\/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\" \/>\u00a0Honorary Scripting Guy, Sean Kearney, is here as we happily go along our scripting way in Azure Resource Manager. Yesterday, we started the process by defining a virtual machine (VM) configuration. Today, we\u2019ll set up the needed storage for this machine.<\/p>\n<p>The cmdlet in charge of all of this is <strong>Set-AzureRMVMOSDisk<\/strong>. It\u2019s called that because there is actually one to\u00a0define a data disk, too. This is incredibly important when you define a virtual machine because it does help you define the roles in advance.<\/p>\n<p>As an example, it\u2019s always a good practice to separate your data from the operating system. Data will have different needs. Perhaps data needs more speed on far more expensive disks. Perhaps data is <u>not<\/u> critical, for example, when you\u2019re using a ripper to convert raw data for graphics conversions. After the files are done, you don\u2019t need to retain the temporary data.<\/p>\n<p>In either case, we need to define the disks that are being attached and, ideally, their purpose.<\/p>\n<p>To define a basic operating system disk, we need to provide three parameters:<\/p>\n<ul>\n<li>a filename<\/li>\n<li>the type of caching that it will need<\/li>\n<li>the storage blob to store it on<\/li>\n<\/ul>\n<p>The file name is pretty easy, of course. You should choose a name that\u2019s unique to the VM so that you don\u2019t clash with other VHD files. You should also consider the possibility that the name that you choose might have been used before. (What? Nobody <u>ever<\/u> called a VM \u201cTest\u201d?)<\/p>\n<p>To have a simple method to avoid a clash with other files, why not add some details to the file name that should make it a bit more unique, such as:<\/p>\n<ul>\n<li>Your initials<\/li>\n<li>A department code<\/li>\n<li>Today\u2019s date and time<\/li>\n<li>The purpose of the disk (operating system or data)<\/li>\n<li>The VM name<\/li>\n<\/ul>\n<p>I\u2019m not suggesting all of these, but in today\u2019s example, we\u2019ll use at least the machine name and our initials to start and follow that by adding the type of disk (operating system or data).<\/p>\n<p>If you remember from yesterday, we defined the VM name in the following manner:<\/p>\n<p style=\"padding-left: 30px\"><code>$VMName=\u2019HSG-Server1\u2019<\/code><\/p>\n<p>From this point, let\u2019s identify our initials and the type of disk, and build out the file name:<\/p>\n<p style=\"padding-left: 30px\"><code>$Initials=\u2019HSG\u2019\n$Disk=\u2019OS\u2019\n$Diskname=$VMName+\u2019_\u2019+\u2019$Initials+\u2019_\u2019+$Disk<\/code><\/p>\n<p>We next need to define the caching option for this disk. If you remember, when we grabbed the VM object from last week, we used it to obtain information that we needed.<\/p>\n<p>The easiest way that I&#8217;ve found to get the type of caching for the disk is to reference one from a previous VM. This is stored under the <strong>StorageProfile<\/strong> property. You can access it in the following manner, and we\u2019ll use last week\u2019s VM as the example:<\/p>\n<p style=\"padding-left: 30px\"><code>$VM=Get-AzureRMVM \u2013name HSG-Linux1 \u2013ResourceGroupName HSG-AzureRG\n$VM.StorageProfile.OSDisk.Caching<\/code><\/p>\n<p>As last week, we can pipe that into the clipboard or copy \/ paste the information or just type it:<\/p>\n<p style=\"padding-left: 30px\"><code>$Caching = 'ReadWrite'<\/code><\/p>\n<p>Our next challenge is to actually put this <u>into<\/u> the storage blob in question. If you wanted to use an existing blob that another VM was on, we would use last week\u2019s technique by scooping the name from the original VM object:<\/p>\n<p style=\"padding-left: 30px\"><code>$VM.StorageProfile.OSDisk.vhd.uri<\/code><\/p>\n<p>From that point, you could do a little splitting and substring manipulation. The other approach is to identify a storage blob that you\u2019d like to use with the <strong>Get-AzureRMStorageAccount<\/strong> cmdlet. We can list all available storage accounts by just running this cmdlet.<\/p>\n<p>If you\u2019d like to show only the ones that are available for a particular resource group, you can supply the particular resource group as a parameter:<\/p>\n<p style=\"padding-left: 30px\"><code>Get-AzureRMStorageAccount \u2013ResourceGroupName HSG-AzureRG<\/code><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-060716.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78755\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-060716.png\" alt=\"Screenshot that that shows result when you add a resource group as a parameter to Get-AzureRMStorageAccount to return a specific storage account.\" width=\"617\" height=\"129\" \/><\/a><\/p>\n<p>Now, if you need to filter on this because you could have multiple accounts, you\u2019ll need to run this through a <strong>Where-Object<\/strong>. We can filter on the <strong>StorageAccountName<\/strong> property. In the following example, we are looking for storage accounts with the letters, hsg, in the name:<\/p>\n<p style=\"padding-left: 30px\"><code>Get-AzureRmStorageAccount -ResourceGroupName HSG-AzureRG | Where { $_.StorageAccountName -match 'hsg' }<\/code><\/p>\n<p>After we have a storage account object, we need to grab the blob URI from it. This is a part of the <strong>PrimaryEndpoints<\/strong> property, which we can access in the following manner:<\/p>\n<p style=\"padding-left: 30px\"><code>$Storage=Get-AzureRmStorageAccount -ResourceGroupName HSG-AzureRG | Where { $_.StorageAccountName -match 'hsg' }\n$StorageGroupURL=$Storage.PrimaryEndpoints.Blob+\u2019\/vhds\/\u2019<\/code><\/p>\n<p>You\u2019ll notice that we added \u2018vhds\u2019 to the name. That\u2019s because, when we create a VM, it\u2019s where Azure Resource Manager expects the data to be. In this manner, it matches the source URI from last week.<\/p>\n<p>We now pull all the pieces together and assemble the object for the disk. Just as yesterday, it doesn\u2019t actually do anything to Azure yet. We\u2019re just building an object that will be used to eventually attach to a cmdlet to spin up the VM:<\/p>\n<p style=\"padding-left: 30px\"><code>$DiskURI=$StorageAccountURL+$DiskName+\u2019.vhd\u2019\n$AzureVM = Set-AzureRmVMOSDisk -VM $AzureVM -VhdUri $DiskURI -name $DiskName -CreateOption fromImage -Caching $Caching<\/code><\/p>\n<p>At this point, the next part of the journey has been completed. Tomorrow, we start wiring up a network card, virtual, of course, on this machine.<\/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: Use the Azure Resource Manager cmdlets to define the operating system disk on a storage blob for a virtual machine. This blog post is part of a series about how to create Azure Resource Manager virtual machines by using PowerShell. To get the most out of this series, read the posts in order. Create [&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-78745","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 the Azure Resource Manager cmdlets to define the operating system disk on a storage blob for a virtual machine. This blog post is part of a series about how to create Azure Resource Manager virtual machines by using PowerShell. To get the most out of this series, read the posts in order. Create [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78745","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=78745"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78745\/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=78745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=78745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=78745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}