Use PowerShell to Create Virtual Machine in Azure – Part 4

Doctor Scripto

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’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:

If you didn’t already know, Windows PowerShell 4.0 introduced a great new feature called Desired State Configuration, which allows you to define and enforce the features that a server requires. Here is a simple example we will be using to define a very basic web server with ASP.NET:

configuration IISInstall
{
 node “Server01”
 {
  WindowsFeature IIS
  {
   Ensure = “Present”
   Name = “Web-Server”     
  }
  WindowsFeature IIS-ASP
  {
   Ensure = “Present”
   Name = “Web-Asp-Net45”     
  }
 }
} By using Desired State Configuration, we could apply this against a machine called “Server01” and enable the web server feature in addition to ASP.NET 4.5. Now for the fun part… This 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 “Server01” to “localhost”. Here is a simple example we will be using to define a very basic web server with ASP.NET.

configuration BasicASPWebSite
{
 node “localhost”
 {
  WindowsFeature IIS
  {
   Ensure = “Present”
   Name = “Web-Server”     
  }
  WindowsFeature IIS-ASP
  {
   Ensure = “Present”
   Name = “Web-Asp-Net45”     
  }
 }
} Let’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:

Publish-AzureVMDSCConfiguration If the file was saved locally under C:Config, we would transfer it in the following manner:

Publish-AzureVMDSCConfiguration –configurationpath C:ConfigDSCWEBConfig.PS1 Last time, we used the following script to create a virtual machine named ‘HSG-VM1’. We simply need to add in one additional line:

# Get information needed for Windows Server 2012 R2 image name from Azure

$VMimage=((Get-AzureVMImage | where { $_.Label -like ‘Windows Server 2012 R2*’ } |
Sort-Object PublishedDate)[-1]).ImageName

# Get the information for instance size A0

$Size=(Get-AzureRoleSize | Where { $_.InstanceSize –match ‘A0’ }).InstanceSize

# Virtual machine name

$VMName=’HSG-VM1′

$VMconfig=New-AzureVMConfig –Name $VMName -InstanceSize $Size -ImageName $VMimage

$Adminname=’HSGAdmin’

$AdminPass=Convertto-Securestring –asplaintext ‘NotP@ssw0rd’ -force

$VMProvision = $VMconfig |
Add-AzureProvisioningConfig –windows –password $AdminPass –AdminUserName $AdminName

$VMResult=$VMProvision | New-AzureVM –Servicename ‘HSG-Service’ –waitforboot To 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 New-AzureVM and Set-AzureVMDSCExtension. 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.

$VMDSCConfig=$VMProvision | Set-AzureVMDSCExtension  –ConfigurationArchive “DSCWEBConfig.PS1.ZIP”

$VMResult=$VMDSCConfig | New-AzureVM –Servicename ‘HSG-Service’ –waitforboot Our 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.

# Get information needed for Windows Server 2012 R2 image name from Azure

$VMimage=((Get-AzureVMImage | where { $_.Label -like ‘Windows Server 2012 R2*’ } |
Sort-Object PublishedDate)[-1]).ImageName

# Get the information for instance size A0

$Size=(Get-AzureRoleSize | Where { $_.InstanceSize –match ‘A0’ }).InstanceSize

# Virtual machine name

$VMName=’HSG-VM1′

$VMconfig=New-AzureVMConfig –Name $VMName -InstanceSize $Size -ImageName $VMimage

$Adminname=’HSGAdmin’

$AdminPass=Convertto-Securestring –asplaintext ‘NotP@ssw0rd’ -force

$VMProvision = $VMconfig |
Add-AzureProvisioningConfig –windows –password $AdminPass –AdminUserName $AdminName

$VMDSCConfig=$VMProvision | Set-AzureVMDSCExtension –ConfigurationArchive “DSCWEBConfig.PS1.ZIP”

$VMResult=$VMDSCConfig | New-AzureVM –Servicename ‘HSG-Service’ –waitforboot Now imagine this cool capability. You can use DSC to create your development and test environments in Microsoft Azure. When you’re done, you can use the SAME file to spin up your production systems, regardless of location. Pop in tomorrow when I’ll show you the last cool bit about spinning up virtual machines in Azure: creating your very own template to use. I invite you to follow The Scripting Guys on Twitter and Facebook. If you have any questions, send an email to The Scripting Guys at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, remember eat your cmdlets every day with a dash of creativity. Sean Kearney, Windows PowerShell MVP and Honorary Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon