{"id":6031,"date":"2015-05-21T00:01:00","date_gmt":"2015-05-21T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/05\/21\/use-powershell-to-create-virtual-machine-in-azure-part-4\/"},"modified":"2019-02-18T09:47:51","modified_gmt":"2019-02-18T16:47:51","slug":"use-powershell-to-create-virtual-machine-in-azure-part-4","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-virtual-machine-in-azure-part-4\/","title":{"rendered":"Use PowerShell to Create Virtual Machine in Azure &#8211; Part 4"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Learn how to leverage Desired State Configuration with a virtual machine in Microsoft Azure.<\/span>\nHonorary Scripting Guy, Sean Kearney, here today filling in for my buddy Ed. Today I&#8217;ll show you how to leverage Desired State Configuration (DSC) with a virtual machine in Microsoft Azure. This post is part of a series. To catch up, read:<\/p>\n<ul>\n<li><a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-virtual-machine-in-azure-part-1\/\" target=\"_blank\">Use PowerShell to Create Virtual Machine in Azure &ndash; Part 1<\/a><b><\/b><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2015\/05\/19\/use-powershell-to-create-virtual-machine-in-azure-part-2.aspx\" target=\"_blank\">Use PowerShell to Create Virtual Machine in Azure &ndash; Part 2<\/a><b><\/b><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2015\/05\/20\/use-powershell-to-create-virtual-machine-in-azure-part-3.aspx\" target=\"_blank\">Use PowerShell to Create Virtual Machine in Azure &ndash; Part 3<\/a><\/li>\n<\/ul>\n<p>If you didn&#8217;t already know, Windows PowerShell&nbsp;4.0 introduced a great new feature called Desired State Configuration, which allows you to define and enforce the features that a server requires.\nHere is a simple example we will be using to define a very basic web server with ASP.NET:<\/p>\n<p style=\"margin-left:30px\">configuration&nbsp;IISInstall <br \/> { <br \/> &nbsp;node&nbsp;&#8220;Server01&#8221; <br \/> &nbsp;{ <br \/> &nbsp; WindowsFeature&nbsp;IIS <br \/> &nbsp; { <br \/> &nbsp;&nbsp; Ensure&nbsp;=&nbsp;&#8220;Present&#8221; <br \/> &nbsp;&nbsp; Name&nbsp;=&nbsp;&#8220;Web-Server&#8221;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/> &nbsp; } <br \/> &nbsp; WindowsFeature&nbsp;IIS-ASP<br \/> &nbsp; { <br \/> &nbsp;&nbsp; Ensure&nbsp;=&nbsp;&#8220;Present&#8221; <br \/> &nbsp;&nbsp; Name&nbsp;=&nbsp;&#8220;Web-Asp-Net45&#8221;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/> &nbsp; } <br \/> &nbsp;} <br \/> }\nBy using Desired State Configuration, we could apply this against a machine called &#8220;Server01&#8221; and enable the web server feature in addition to ASP.NET 4.5.\nNow for the fun part&#8230;\nThis very same file, which defines a server configuration, needs only one change to be leveraged on a virtual machine in Azure. Change the name of the node from &#8220;Server01&#8221; to &#8220;localhost&#8221;.\nHere is a simple example we will be using to define a very basic web server with ASP.NET.<\/p>\n<p style=\"margin-left:30px\">configuration&nbsp;BasicASPWebSite <br \/> { <br \/> &nbsp;node&nbsp;&#8220;localhost&#8221; <br \/> &nbsp;{ <br \/> &nbsp; WindowsFeature&nbsp;IIS <br \/> &nbsp; { <br \/> &nbsp;&nbsp; Ensure&nbsp;=&nbsp;&#8220;Present&#8221; <br \/> &nbsp;&nbsp; Name&nbsp;=&nbsp;&#8220;Web-Server&#8221;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/> &nbsp; } <br \/> &nbsp; WindowsFeature&nbsp;IIS-ASP<br \/> &nbsp; { <br \/> &nbsp;&nbsp; Ensure&nbsp;=&nbsp;&#8220;Present&#8221; <br \/> &nbsp;&nbsp; Name&nbsp;=&nbsp;&#8220;Web-Asp-Net45&#8221;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/> &nbsp; } <br \/> &nbsp;} <br \/> }\nLet&#8217;s imagine that we saved this file as DSCWEBConfig.PS1. To leverage this with Azure, we now only need two cmdlets. The first sends the file up to Azure:<\/p>\n<p style=\"margin-left:30px\">Publish-AzureVMDSCConfiguration\nIf the file was saved locally under C:Config, we would transfer it in the following manner:<\/p>\n<p style=\"margin-left:30px\">Publish-AzureVMDSCConfiguration &ndash;configurationpath C:ConfigDSCWEBConfig.PS1\nLast time, we used the following script to create a virtual machine named &#8216;HSG-VM1&#8217;. We simply need to add in one additional line:<\/p>\n<p style=\"margin-left:30px\"># Get information needed for Windows Server 2012 R2 image name from Azure<\/p>\n<p style=\"margin-left:30px\">$VMimage=((Get-AzureVMImage | where { $_.Label -like &#8216;Windows Server 2012 R2*&#8217; } | <br \/>Sort-Object PublishedDate)[-1]).ImageName<\/p>\n<p style=\"margin-left:30px\"># Get the information for instance size A0<\/p>\n<p style=\"margin-left:30px\">$Size=(Get-AzureRoleSize | Where { $_.InstanceSize &ndash;match &#8216;A0&#8217; }).InstanceSize<\/p>\n<p style=\"margin-left:30px\"># Virtual machine name<\/p>\n<p style=\"margin-left:30px\">$VMName=&#8217;HSG-VM1&#8242;<\/p>\n<p style=\"margin-left:30px\">$VMconfig=New-AzureVMConfig &ndash;Name $VMName -InstanceSize $Size -ImageName $VMimage<\/p>\n<p style=\"margin-left:30px\">$Adminname=&#8217;HSGAdmin&#8217;<\/p>\n<p style=\"margin-left:30px\">$AdminPass=Convertto-Securestring &ndash;asplaintext &#8216;NotP@ssw0rd&#8217; -force<\/p>\n<p style=\"margin-left:30px\">$VMProvision = $VMconfig | <br \/>Add-AzureProvisioningConfig &ndash;windows &ndash;password $AdminPass &ndash;AdminUserName $AdminName<\/p>\n<p style=\"margin-left:30px\">$VMResult=$VMProvision | New-AzureVM &ndash;Servicename &#8216;HSG-Service&#8217; &ndash;waitforboot\nTo leverage the DSC we have uploaded, we need to inject one additional line into the script before we finalize the creation of the virtual machine by using <b>New-AzureVM<\/b> and <b>Set-AzureVMDSCExtension<\/b>. We need to provide the name of the DSC script as a parameter with a .zip extension because the previous cmdlet actually zips the DSC file before sending it.<\/p>\n<p style=\"margin-left:30px\">$VMDSCConfig=$VMProvision | Set-AzureVMDSCExtension<b>&nbsp;<\/b>&nbsp;&ndash;ConfigurationArchive &#8220;DSCWEBConfig.PS1.ZIP&#8221;<\/p>\n<p style=\"margin-left:30px\">$VMResult=$VMDSCConfig | New-AzureVM &ndash;Servicename &#8216;HSG-Service&#8217; &ndash;waitforboot\nOur final script will look like this. It will create a Windows Server 2012 R2 image preconfigured with ASP.NET and IIS, based on our DSC file.<\/p>\n<p style=\"margin-left:30px\"># Get information needed for Windows Server 2012 R2 image name from Azure<\/p>\n<p style=\"margin-left:30px\">$VMimage=((Get-AzureVMImage | where { $_.Label -like &#8216;Windows Server 2012 R2*&#8217; } | <br \/>Sort-Object PublishedDate)[-1]).ImageName<\/p>\n<p style=\"margin-left:30px\"># Get the information for instance size A0<\/p>\n<p style=\"margin-left:30px\">$Size=(Get-AzureRoleSize | Where { $_.InstanceSize &ndash;match &#8216;A0&#8217; }).InstanceSize<\/p>\n<p style=\"margin-left:30px\"># Virtual machine name<\/p>\n<p style=\"margin-left:30px\">$VMName=&#8217;HSG-VM1&#8242;<\/p>\n<p style=\"margin-left:30px\">$VMconfig=New-AzureVMConfig &ndash;Name $VMName -InstanceSize $Size -ImageName $VMimage<\/p>\n<p style=\"margin-left:30px\">$Adminname=&#8217;HSGAdmin&#8217;<\/p>\n<p style=\"margin-left:30px\">$AdminPass=Convertto-Securestring &ndash;asplaintext &#8216;NotP@ssw0rd&#8217; -force<\/p>\n<p style=\"margin-left:30px\">$VMProvision = $VMconfig | <br \/>Add-AzureProvisioningConfig &ndash;windows &ndash;password $AdminPass &ndash;AdminUserName $AdminName<\/p>\n<p style=\"margin-left:30px\">$VMDSCConfig=$VMProvision | <span>Set-AzureVMDSCExtension<\/span>&nbsp;&ndash;ConfigurationArchive &#8220;DSCWEBConfig.PS1.ZIP&#8221;<\/p>\n<p style=\"margin-left:30px\">$VMResult=$VMDSCConfig | New-AzureVM &ndash;Servicename &#8216;HSG-Service&#8217; &ndash;waitforboot\nNow imagine this cool capability. You can use DSC to create your development and test environments in Microsoft Azure. When you&#8217;re done, you can use the SAME file to spin up your production systems, regardless of location.\nPop in tomorrow when I&#8217;ll show you the last cool bit about spinning up virtual machines in Azure: creating your very own template to use.\nI 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 an email to The Scripting Guys at <a href=\"http:\/\/blogs.technet.commailto: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. Until then, remember eat your cmdlets every day with a dash of creativity.\n<b>Sean Kearney, <\/b>Windows PowerShell MVP and Honorary Scripting Guy<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to leverage Desired State Configuration with a virtual machine in Microsoft Azure. Honorary Scripting Guy, Sean Kearney, here today filling in for my buddy Ed. Today I&#8217;ll show you how to leverage Desired State Configuration (DSC) with a virtual machine in Microsoft Azure. This post is part of a series. To catch [&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":[1],"tags":[56,154,549,45],"class_list":["post-6031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-sean-kearney","tag-series","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to leverage Desired State Configuration with a virtual machine in Microsoft Azure. Honorary Scripting Guy, Sean Kearney, here today filling in for my buddy Ed. Today I&#8217;ll show you how to leverage Desired State Configuration (DSC) with a virtual machine in Microsoft Azure. This post is part of a series. To catch [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6031","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=6031"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6031\/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=6031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=6031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=6031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}