Azure Resource Manager IaaS via PowerShell

Azure Resource Manager IaaS via PowerShell

Recently, I have been working through the Managing Azure IaaS with PowerShell class by Elton Stoneman. This is hands down the best class I have taken on Azure. It gets right to the heart of becoming an Azure wizard with PowerShell. I highly recommend taking the class yourself.

Below are my notes from the first module of the class for reference in the future.

Setting Up Azure PowerShell

To install the Azure Resource Manager PowerShell module you need to be using at least PowerShell 3 or above on a Windows based OS.

You will also need PowerShellGet installed. This comes preinstalled on Windows 10, but you can install it manually. PowerShellGet is similar to NuGet for .NET.

There are three steps needed to begin using Azure PowerShell:

Allow PS scripts to be executed on your machine by running the Set-ExecutionPolicy command while running in an elevated instance of PowerShell.

Set-ExecutionPolicy Unrestricted

Then install Azure PowerShell via PowerShellGet using the Install-Module command. This will take a while, so go get some coffee.

Install-Module AzureRM

Finally, we need to authenticate to Azure using the Login-AzureRmAccount. This will popup an authentication dialog where you use your Azure credentials.


You can view the subscriptions available to this account with Get-AzureRmSubscription. If you have more than one, you can set one to default with Select-AzureRmSubscription.

Get-AzureRmSubscription –SubscriptionName "Free Trial" | 

Creating a Resource Group & Storage Account

You can create an Azure Resource Group and then attach any virtual machine, load balancer or other resource to it. This is a nice way to package all the resources for a given project together. You can even remove everything easily, but deleting the resource group. All the resources attached to it will be deleted as well.

To create a Resource Group, you need to give it a name and a location. A full list of Data Center Locations can be found here or via the Get-AzureRmLocation command.

Before you can create a virtual machine or any resource that requires storage, you will need a Storage Account.

$location = "West US"
$resourceGroupName = "funky-bunch"
$storageAccountName = "funky-bunch-storage"

New-AzureRmResourceGroup -Name $resourceGroupName `
    -Location $location

$storageAccount = New-AzureRmStorageAccount -Name $storageAccountName `
    -ResourceGroupName $resourceGroupName `
    -Location $location `
    -Type Standard_LRS 

Note: For the remainder of this post, I will assume that the variables that are defined are available for the rest of the post. The variables $location, $resourceGroupName, $storageAccountName and $storageAccount will be used below.

Creating a Virtual Network

$virtualNetworkName = "funky-bunch-net"

$subnet = New-AzureRmVirtualNetworkSubnetConfig -Name frontendSubnet `

$virtualNetwork = New-AzureRmVirtualNetwork -Name $virtualNetworkName `
    -ResourceGroupName $resourceGroupName `
    -Location $location `
    -Subnet $subnet `

Creating a Public IP & Network Interface

$networkInterfaceName = "vm1-nic"

$publicIP = New-AzureRmPublicIpAddress -Name $networkInterfaceName `
    -ResourceGroupName $resourceGroupName `
    -Location $location `
    -AllocationMethod Dynamic

$networkInterface = New-AzureRmNetworkInterface -Name $networkInterfaceName `
    -ResourceGroupName $resourceGroupName `
    -Location $location `
    -SubnetId $virtualNetwork.Subnets[0].Id `
    -PublicIpAddressId $publicIP.Id

Creating a Simple Windows VM

To create a virtual machine, you need a base image to start with. Azure hosts tons of base images to choose from out of the box. Images are offed by Microsoft and other partners. You can get a list of the partners, what the offer and all available SKUs easily enough with Get-AzureRmVMImagePublisher, Get-AzureRmVMImageOffer and Get-AzureRmVMImageSku.

Get-AzureRmVMImagePublisher -Location $location

Get-AzureRmVMImageOffer -PublisherName "MicrosoftWindowsServer" `
    -Location $location
Get-AzureRmVMImageSku -PublisherName "MicrosoftWindowsServer" `
    -Offer "windowsserver" `
    -Location $location 

Once we have a base image selected, we need to create our administration account credentials as well as a location in our storage account to store our virtual machine hard drive image.

$vmName = "funky-bunch-win-web"
$credentials = Get-Credential -Message "Admin Credentials for VM"
$operatingSystemDiskUri = $storageAccount.PrimaryEndpoints.Blob.ToString() `
    + "vhds/" + $vmName + ".vhd"

$vm = NewAzureRmVmConfig -VMName $vmName -VMSize "Basic_A1"

$vm = Set-AzureRmVMOperatingSystem -VM $vm -ComputerName $vmName `
    -Credential $credentials `
    -ProvisionVMAgent -EnableAutoUpdate

$vm = Set-AzureRmVMSourceImage -VM $vm `
    -PublisherName "MicrosoftWindowsServer" `
    -Offer "WindowsServer" `
    -Skus "2012-R2-Datacenter" -Version "latest"

$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $networkInterface.Id

$vm = Set-AzureRmVMOSDisk -VM $vm -Name $vmName -CreateOption fromImage `
    -VhdUri $operatingSystemDiskUri

New-AzureRmVM -VM $vm -ResourceGroupName $resourceGroupName -Location $location

Once complete, you can retrieve details about your VM, Network Interface and Public IP (used to connect via RDP) by using the Get-AzureRmVM, Get-AzureRmNetworkInterface & Get-AzurePublic commands. Each of these commands will return a JSON object containing all the properties of the object you requested.

Get-AzureRmVM -Name $vmName -ResourceGroupName $resourceGroupName

Get-AzureRmNetworkInterface -Name $networkInterfaceName `
    -ResourceGroupName $resourceGroupName

Get-AzureRmPublicIpAddress - Name $networkInterfaceName `
    -ResourceGroupName $resourceGroupName

The process for creating a linux virtual machine is identical, simply use the commands detailed above to find a linux base image.

Get-AzureRmVMImageOffer -PublisherName "Cononical" -Location $location

Get-AzureRmVMImageSku -PublisherName "Cononical" -Offer "UbuntuServer" `
    -Location $location

With these basic building blocks, you can create as intricate of an infrastructure as you need. Completely scripted and reproducible. Elton goes into much greater detail including creating your own custom images, load balancers and ARM templates. You should definitely go check it out.

"The Capitol" By Mari Wirta is licensed under CC BY 2.0

Follow me on Mastodon!