November 20th, 2014

Managing Azure IaaS with Windows PowerShell: Part 4

Doctor Scripto
Scripter

Summary: Create a simple virtual machine from a predefined template in Azure.

Honorary Scripting Guy, Sean Kearney, is here with my head still stuck in the clouds!

Yesterday we created a network in Microsoft Azure for our resources to leverage—much like Hyper-V leverages a virtual network switch for its resources. Today we'll create a simple virtual machine from a predefined template in Azure.

Creating a virtual machine from a template in Azure is pretty simple. We select Virtual Machines in the left pane of the management portal, and then click the +NEW button at the bottom.

Image of menu

From here, we choose Quick Create under the Virtual Machine option. You can choose Custom if you'd like; but for this example, we'll work with a pretty simple configuration.

In our example, we are creating a DNS name as our entry point to the virtual machine. This does not need to reflect the name of the virtual machine, but it should be unique.

Under Image, we select Windows Server 2012 R2 Datacenter, which will load a default template to lay down our operating system.

Azure is bundled with machine "sizes," which are predefined, non-customizable, memory/CPU configurations. In this case, we choose an A0 configuration, which is a CPU that is shared amongst other tenants and has 768 MB of RAM. It's the smallest virtual machine I could get my hands on in Azure—I am "frugally conscious."

…OK, I'm cheap. You got me.

Image of menu

We need to select a Region/Affinity Group as we did previously with the network. We are also being prompted for a unique User name for the default Administrator, and a password for that account. This is because the default Administrator account is disabled.

It is also because, by default, the machine is publicly accessible via RDP on a customized random port number. By ensuring that the user name is unique to you, you can somewhat mitigate the temporary security risk.

You'll also discover that using passwords like "P@ssw0rd" or "Password1" won't fly in the Azure world. It will reject that quicker than you can blink an eye.

Clicking CREATE A VIRTUAL MACHINE will start the process. Give this about 10 minutes, and you will have a fully functioning virtual machine based on a Windows Server 2012 R2 Datacenter template.

If you'd like to do this in a more consistent and automatable fashion with Windows PowerShell, we can leverage a few cmdlets.

First, we create a configuration to define the virtual machine in Azure by using the New-AzureVMConfig cmdlet. We can simply type the template name—or we can get smart and programmatically access the list of names from Azure by using the Get-AzureVMImage cmdlet.

As we noticed before, we can get multiple versions of the images. We can sort the list so that the newest image is at the top of the list of returned objects:

$ImageType=Get-AzureVMImage | where { $_.Label –like 'Windows Server 2012 R2 Datacenter*' } |
Sort-Object CreatedTime

The trick with the New-AzureVMImage cmdlet is that it does not need the name of the image (which in our example is Windows Server 2012 R2 Datacenter). It needs the name of the VHD, and that name is stored under the property called ImageName.

With this in place, we can access only the newest image and pull out the name of the file of our image:

$ImageName=$ImageType[0].ImageName

Now we must obtain the instance size. In the management portal, it is an A0, but the revealed virtual machine name properties show Basic_A0 as the name. Let's get this from Azure rather than trying to remember it:

$Size=((Get-AzureLocation | Where { $_.name -eq 'East US' }).VirtualMachineRoleSizes) | Where { $_ -like '*A0*' }

Now that we have all of the details, we can start the configuration. We need to pass down the virtual machine name, the size in Azure, and the datacenter location:

$AzureVM=New-AzureVMConfig –Name "brandnew1" –instancesize $Size –ImageName $ImageName

We next need to define the User ID and password, which is used to access the virtual machine. (Yes, you are seeing this example in clear text. It's an example that you can build upon.)

We pipe the Azure object into this and build on it:

$Password='SomethingAzur3wontCompl@inAb0ut!'

$AdminUserName='HSGAdministrator'

$AzureVM | Add-AzureProvisioningConfig –Windows –Adminusername $AdminUserName –password $Password

The final piece is to spin up the virtual machine. We need the name of our Azure service and the datacenter location. We can get the name of the Azure service by using the Get-AzureService cmdlet and accessing the ServiceName property:

$Service=(Get-AzureService).Servicename

We now pipe our Azure object for the virtual machine to the New-AzureVM cmdlet:

$AzureVM | New-AzureVM –Servicename $Service –Location 'East US'

As a single script, it would look like this:

# Get details for Virtual Machine template Filename

$ImageType=Get-AzureVMImage | where { $_.Label –like 'Windows Server 2012 R2 Datacenter*' } |
Sort-Object CreatedTime

$ImageName=$ImageType[0].ImageName

$Size=((Get-AzureLocation | Where { $_.name -eq 'East US' }).VirtualMachineRoleSizes) | Where { $_ -like '*A0*' }

# Build Basic Configuration from template

$AzureVM=New-AzureVMConfig –Name "brandnew1" –instancesize $Size –ImageName $ImageName

# Define UserID and Password to inject into Virtual Machine

$Password='SomethingAzur3wontCompl@inAb0ut!'

$AdminUserName='HSGAdministrator'

$AzureVM | Add-AzureProvisioningConfig –Windows –Adminusername $AdminUserName –password $Password

# Obtain name of Azure Service and provision machine within 'East US' Datacenter

$Service=(Get-AzureService).Servicename

$AzureVM | New-AzureVM –Servicename $Service –Location 'East US'

Come back tomorrow when we'll see how to manage these systems from Windows PowerShell, including resetting the system passwords!

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 to eat your cmdlets every day with a dash of creativity.

Sean Kearney, Windows PowerShell MVP and Honorary Scripting Guy 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.