Deploy VMs Using Azure Resource Manager and PowerShell

Posted on March 28, 2016 by Russell Smith in Microsoft Azure with 0 Comments

Tutorial Hero

In today’s Ask the Admin, I’ll show you how to deploy a VM in Azure using the new Resource Manager deployment model and PowerShell.

If you think back to my article Deploy VMs Using Azure Resource Manager on the Petri IT Knowledgebase, you’ll recall that I showed you how to deploy a VM in Azure using the new management portal, which supports a deployment method called Resource Manager.

While Azure Resource Manager (ARM) brings a new level of flexibility to Azure over the classic deployment method, if you followed through the instructions in the link above, you’ll have realized that the process is more complex than before. In fact, ARM reminds me somewhat of deploying VMs in the Amazon cloud, which is more complicated than deploying Azure VMs in classic mode. To get a better understanding of Resource Manager and how it differs from the classic deployment method, see Aidan Finn’s piece A Tale of Two Azures on Petri.

Logging in and setting a subscription to use with Azure Resource Manager (ARM). (Image Credit: Russell Smith)

Logging in and setting a subscription to use with Azure Resource Manager (ARM). (Image Credit: Russell Smith)

So it should come as no surprise that deploying ARM-based VMs using PowerShell is also considerably more complicated than was previously the case. Each resource must be provisioned manually, unlike using the classic-mode cmdlets, where much of the heavy lifting was automated behind the scenes.

The following script deploys a basic A0 VM in Azure using standard storage, a dynamic public IP address and friendly DNS name. A storage account is also created. The script creates a Resource Group (RG), Virtual Network (VNET), and subnet if resources with the names specified in the script variables are not found in the Azure subscription.

Note that before you can work with ARM in PowerShell, you’ll need to install Microsoft Azure PowerShell 1.0 or later. See Microsoft Releases Azure PowerShell 1.0 on Petri for more details.

Sponsored

Deploy VM using PowerShell ARM

First the script needs to log in to ARM, and while most readers will only have one Azure subscription associated with their Microsoft Account, the script selects the first [0] sub listed by the Select-AzureRmSubscription cmdlet.

Next I set variables required throughout the rest of the script, such as the RG name ($rgName), location ($location), VM name, virtual network name ($vnetName), amongst others. The username and password for the VM are converted to a secure string and system object. The $vmName variable should be given in lowercase with no special characters because I reuse it for the storage account name, which has these restrictions placed upon it.

Now let’s create a new RG, if one with the name given in $rgName doesn’t already exist in the Azure subscription:

Create a new Resource Group using PowerShell ARM (Image Credit: Russell Smith)

Create a new Resource Group using PowerShell ARM (Image Credit: Russell Smith)

In a similar manner, a new storage account will be created. The script is aborted if the storage account name isn’t globally unique, i.e. it must not exist in any Azure subscription.

Now we need to set up a VHD file in Azure storage:

The script then determines the latest available image for Windows Server 2012 R2 Datacenter:

Next up is networking, and here I create a public IP address resource with friendly DNS name, and a subnet object so that a virtual network can be created if one doesn’t already exist with the given name:

Here the VM is assigned a Network Interface Card (NIC) resource:

Now we’re ready to provision the VM, using the New-AzureRmVMConfig, Add-AzureRmVMNetworkInterface, Set-AzureRmVMOperatingSystem, Set-AzureRmVMSourceImage, Set-AzureRmVMOSDisk to define the VM’s properties, and New-AzureRmVM to perform the actual deployment job. The New-AzureRmVM cmdlet can take a long time to complete.

VM properties in Azure RM (Image Credit: Russell Smith)

VM properties in Azure RM (Image Credit: Russell Smith)



Finally, the script outputs to the console the URL for connecting to the new VM using RDP:

RDP connection string to the new VM (Image Credit: Russell Smith)

RDP connection string to the new VM (Image Credit: Russell Smith)

Sponsored

You can now use this script to quickly deploy VMs, and associated resources, in Azure without having to wade through the wizards in the web management portal. Keep a look out on Petri for some forthcoming articles on how to deploy domain controllers and member servers using PowerShell ARM.

Sponsored

Tagged with , , ,