Create an Azure VM with managed disks using PowerShell

The following discussion of Azure virtual machines (VMs) and Azure managed disks may be no surprise to you. But it was to me. 

Until sometime in mid-2017, PowerShell scripts that create VMs usually created virtual hard disks (VHDs) in storage accounts. So you might use something like this for the storage account and VHDs that were to be created from the Azure Marketplace Windows Server images:

$VmCred = Get-Credential
$Vm1 = "SampleVMName"
$Rg = "SampleRgName"
$Loc = "eastus"
$Nic1 = Get-AzureRmNetworkInterface -Name "Nic1" -ResourceGroupName $Rg
$OsDisk1Name = $Vm1 + "OsDisk"
$StorageAccount = ($RgPrefix + "Storage").ToLower()
$StorageAccountType = "Standard_LRS"

# Create storage account
$StgAcct = New-AzureRmStorageAccount -ResourceGroupName $Rg -Name $StorageAccount -Type $StorageAccountType -Location $Loc

# Set up a pretty name for the VM's disk in the storage account
$OSDisk1Uri = $($StgAcct.PrimaryEndpoints.Blob.ToString() + "vhds/" + $OsDisk1Name + ".vhd").ToLower()

# Create PSVirtualMachine objects for New-AzureRmVm using
#  Windows Server 2012 R2 image from Azure marketplace on
#  unmanaged disks in a storage account
$Vm1Config = New-AzureRmVMConfig -VMName $Vm1 -VMSize Standard_B2MS | `
Set-AzureRmVMOperatingSystem -Windows -ComputerName $Vm1 -Credential $VmCred | `
Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter -Version latest | `
Add-AzureRmVMNetworkInterface -Id $Nic1.Id | Set-AzureRmVMOSDisk -Name "$Vm1.vhd"-VhdUri $OSDisk1Uri -CreateOption FromImage

# Create the virtual machine
New-AzureRmVM -ResourceGroupName $Rg -Location $Loc -VM $Vm1Config

I figured it would be easy to update PowerShell code like this to create managed disks — and it is, but I made it hard on myself. I kept trying various combinations of Set-AzureRmVmOsDisk with and without -VhdUri along with Set-AzureRmVmOperatingSystem to create a managed disk using the then-current Windows Server image from the Marketplace. But try as I might, I couldn’t stumble on a combination that worked.

Then, lightning struck (thereby temporarily illuminating the dark corners of my brain 🙂 ), and I wondered, “What if Microsoft has made managed disks the default for new VM’s VHDs in PowerShell and didn’t tell anybody?

Voila! Check out the modified version of the script below. 

$VmCred = Get-Credential
$Vm1 = "SampleVMName"
$Rg = "SampleRgName"
$Loc = "eastus"
$Nic1 = Get-AzureRmNetworkInterface -Name "Nic1" -ResourceGroupName $Rg

# Create PSVirtualMachine objects for New-AzureRmVm using
#  Windows Server 2012 R2 image from Azure marketplace on
#  unmanaged disks in a storage account
$Vm1Config = New-AzureRmVMConfig -VMName $Vm1 -VMSize Standard_B2MS | `
    Set-AzureRmVMOperatingSystem -Windows -ComputerName $Vm1 -Credential $VmCred | `
    Add-AzureRmVMNetworkInterface -Id $Nic1.Id | `
    Set-AzureRmVMOSDisk -Name "$Vm1.vhd" | `
    Set-AzureRmVMBootDiagnostics -Disable | `
    Set-AzureRmVMSourceImage -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2012-R2-Datacenter" -Version "latest"

# Create the virtual machines
New-AzureRmVM -ResourceGroupName $Rg -Location $Loc -VM $Vm1Config

Not only is this less code but as long as you don’t need boot diagnostics, you don’t need an Azure storage account at all. That’s one of the major advantages of managed disks — Azure makes sure that redundancy options for the VHDs are automatically spread across fault domains. Plus managed disks make it incredibly easy to create snapshots of the VM’s disks using New-AzureRmSnapshot.


Posted

in

, , ,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *