{"id":78825,"date":"2016-06-09T00:01:35","date_gmt":"2016-06-09T07:01:35","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=78825"},"modified":"2019-02-18T09:10:38","modified_gmt":"2019-02-18T16:10:38","slug":"create-azure-resource-manager-virtual-machines-by-using-powershell-part-4","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-4\/","title":{"rendered":"Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 4"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use the Azure Resource Manager cmdlets to select the operating system image 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><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/06\/07\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-2\/\">Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 2<\/a><\/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>Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 4<\/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\" \/> I\u2019ve got most of that virtual machine (VM) assembled, but I was having quite a time trying to pull together the operating system image with PowerShell. Nudge me in the right direction, could you?<\/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 today to get you a little more information about how to build your own Azure VM within the Resource Manager. Our next big piece is to add in the operating system.<\/p>\n<p>But, before I get much further, today is my son\u2019s birthday. So for a few blinking moments, allow me this opportunity to wish him a Very Happy Birthday. Happy Birthday, Son!<\/p>\n<p>When you worked with earlier versions of the PowerShell cmdlets for Azure, you would just run the <strong>Get-AzureVMImage<\/strong> cmdlet. This one cmdlet would list <u>every<\/u> single image that\u2019s available in the gallery.<\/p>\n<p>The problem here is that with 979 images at last count, it would be a massive pause to get them all. Furthermore, consider that you might only want the Microsoft or Linux images? You still had to pull down this large list and then filter it.<\/p>\n<p>When we created out first Linus VM, we pulled out the properties of the VM in the following manner:<\/p>\n<p style=\"padding-left: 30px\"><code>$VM=Get-AzureRMVMImage \u2013name HSG-Linux1 \u2013resourcegroupname HSG-AzureRG<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$Publisher=$VM.StorageProfile.ImageReference.Publisher<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$Offer=$VM.StorageProfile.ImageReference.Offer<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$Sku=$VM.StorageProfile.ImageReference.Sku<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$Version=$VM.StorageProfile.ImageReference.Version<\/code><\/p>\n<p>These are far more manageable collections because the images themselves are now categorized. These four categories are:<\/p>\n<ul>\n<li>PublisherName<\/li>\n<li>Offer<\/li>\n<li>Sku<\/li>\n<li>Version<\/li>\n<\/ul>\n<h4>PublisherName<\/h4>\n<p>This property contains a descriptive name for the vendor that holds images. However, it\u2019s not as obvious as \u201cMicrosoft\u201d or \u201cLinux\u201d in many cases. This is where I find creating a VM first helps. It contains all these properties that I can filter out.<\/p>\n<p>You can obtain the complete list by using <strong>Get-AzureRMVMImagePublisher<\/strong>. Because the list of images is different among locations, you\u2019ll need to provide a location.<\/p>\n<p style=\"padding-left: 30px\"><code>Get-AzureRMVMImagePublisher \u2013location \u2018eastus\u2019<\/code><\/p>\n<p>In the case of our Linux VM, the publisher is \u2018Canonical\u2019. Yes, I don\u2019t quite get that one either, That\u2019s why I personally find creating a VM first and pulling its properties far easier.<\/p>\n<h4>Offer<\/h4>\n<p>In most cases, this will typically be the base name of the operating system, such as \u201cUbuntu Server\u201d or \u201cWindows Server\u201d. I can, for example, use an additional cmdlet if I supply the <strong>PublisherName<\/strong> to list all available offers. That cmdlet is <strong>Get-AzureRMVMImageOffer<\/strong>, and it requires both a publisher and location.<\/p>\n<p style=\"padding-left: 30px\"><code>Get-AzureRMVMImageOffer \u2013location \u2018eastus\u2019 \u2013PublisherName \u2018Canonical\u2019<\/code><\/p>\n<p>From our last VM, the actual offer name was \u2018UbuntuServer\u2019.<\/p>\n<h4>Sku<\/h4>\n<p>When you target a particular <strong>PublisherName<\/strong> and <strong>Offer<\/strong>, you\u2019ll next target a SKU. Think of the SKU as a refinement\u00a0of the list, such as Server 2012 R2, Datacenter Edition, or Standard Edition.<\/p>\n<p>To get a list of available SKUs, you\u2019ll need to supply the <strong>PublisherName<\/strong>, <strong>Offer<\/strong>, and <strong>Location<\/strong> to the <strong>Get-AzureRMVMImageSKU<\/strong> cmdlet.<\/p>\n<p style=\"padding-left: 30px\"><code>Get-AzureRMVMImageSKU \u2013location \u2018eastus\u2019 \u2013PublisherName \u2018Canonical\u2019 \u2013offer \u2018UbuntuServer\u2019<\/code><\/p>\n<p>What you\u2019ll notice each time is that the list is smaller and quicker to pull down. Also, as long as you know the particular breakdown, it\u2019s easy to switch a VM script by just supplying these first three pieces. It\u2019s also much faster now to pull down the list of images.<\/p>\n<h4>Version<\/h4>\n<p>This last property is closer to what you might think of as the patch level. To identify available versions, you need to supply all the first three properties to the <strong>Get-AzureRMVMImage<\/strong> cmdlet. This will give you an identified list of images from which you\u2019ll need to select the version.<\/p>\n<p>Here\u2019s where the VM won\u2019t help you much. When you pull its version property, you\u2019ll often see this value, Latest.<\/p>\n<p>But if you try to target <u>that<\/u> against a \u201cWhere-object\u201d, you\u2019ll never get it. If a version is <u>not<\/u> supplied, we can sort the images against the <strong>Version<\/strong> object. In the following example, we pull all the available images and target the \u201cLatest\u201d or newest in the following manner.<\/p>\n<p style=\"padding-left: 30px\"><code>$Version=(Get-AzureRMVMImage \u2013location \u2018eastus\u2019 \u2013PublisherName \u2018Canonical\u2019 \u2013Offer \u2018UbuntuServer\u2019 \u2013skus \u201914.04.4-LTS\u2019 | Sort-object Version)[-1].Version<\/code><\/p>\n<p>After we have all of this information, the trick is to add it to a VM image. This is done through the use of the <strong>Set-AzureRMVMSoureImage<\/strong> cmdlet. If you have all of these four properties, you can supply them in the following manner:<\/p>\n<p>In the following example, we add them to our existing VM image object:<\/p>\n<p style=\"padding-left: 30px\"><code>$Publisher=\u2019Canonical\u2019<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$Offer=\u2019UbuntuServer\u2019<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$Sku=\u2019 14.04.4-LTS\u2019<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$Version=\u2019 14.04.201605160\u2019<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$AzureVM = Set-AzureRmVMSourceImage -VM $AzureVM -PublisherName $Publisher -Offer $Offer -Skus $Skus -Version $VMImage.Version<\/code><\/p>\n<p>Using the same process from last week, I spun up a Server 2016 image from MSDN. Here is an example of the same image properties from a Microsoft environment:<\/p>\n<p style=\"padding-left: 30px\"><code>$Publisher=\u2019MicrosoftWindowsServer\u2019<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$Offer=\u2019WindowsServer\u2019<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$Sku=\u2019 Windows-Server-Technical-Preview\u2019<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$Version=\u2019 5.0.20160420\u2019<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$AzureVM = Set-AzureRmVMSourceImage -VM $AzureVM -PublisherName $Publisher -Offer $Offer -Skus $Sku -Version $VMImage.Version<\/code><\/p>\n<p>At this point, there is only one small remaining piece. For that, return tomorrow.<\/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 select the operating system image 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 Azure Resource Manager virtual [&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-78825","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 select the operating system image 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 Azure Resource Manager virtual [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78825","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=78825"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78825\/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=78825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=78825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=78825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}