Azure Automation: How to add a data disk to an Azure VM w/PowerShell

Cover

This article is about two new functions from my Azure Automation Az-ModuleGet-AzVmDisk and New-AzVmDisk.

The built-in Get-AzureRmVm cmdlet returns a VM object in the JSON format. The Get-AzVmDisk function gets this output and makes with it three actions:

  • Cuts disks related data

  • Parses this data

  • Returns PowerShell like structured objects

Get-AzVmDisk

  • For multiple VM

Get-AzureRmVM -ResourceGroupName $AzResourceGroup | Get-AzVmDisk
Get-AzureRmVM -ResourceGroupName $AzResourceGroup | Get-AzVmDisk –DiskType OSDisk
Get-AzureRmVM -ResourceGroupName $AzResourceGroup | Get-AzVmDisk DataDisk | Format-Table -au
  • Interactively for a single VM
Select-AzResourceGroup | Select-AzObject VM | Get-AzVmDisk
Select-AzObject VM | Get-AzVmDisk

The Get-AzVmDisk function returns structured objects with the following properties.

Property Description
VM Virtual Machine name
VMSize VM size (restricts maximum data disk number)
DiskName Logical disk name (disk object name)
DiskType Disk type (OSDisk/DataDisk)
Lun Data disk LUN (Logical Unit Number), -1 for OSDisk
StorageAccount Disk’s parent StorageAccount name
Container Blob container (aka parent folder name)
Vhd Physical disk file name (*.vhd file)
Path Full disk file path
SizeGB Disk file size (GiB)
Cache Disk caching mode (None/ReadOnly/ReadWrite)
Created Disk creation method (Attach/FromImage/Empty)
  • You can select only properties you need with -ExcludeProperty parameter of Select-Object cmdlet. If you are using my New-AzCredProfile function, you have the $AzResourceGroup variable already set.
Get-AzureRmVM -wa SilentlyContinue -ResourceGroupName $AzResourceGroup | Get-AzVmDisk | select * -exclude Container, Path | Format-Table -au

01.Get-AzVmDisk

New-AzVmDisk

  • The New-AzVmDisk function calls the Get-AzVmDisk function twice, before and after adding a new disk.

02.New-AzVmDisk_BeforeAfter

  • For OSDisk disk type the Lun always will be equal to -1 (this property is not relevant for system disks) and disk size SizeGB will be 0 (zero). The Microsoft does not supply this information Emoj. At least I have not found it…
    03.New-AzVmDisk_PortalOSDisk

Disk caching mode

  • If you are not sure what caching mode to choice -Caching parameter (None/ReadOnly/ReadWrite) for your disk, you have an alternative method to choice the disk purpose (SqlData/SqlTempDB/SqlLog) and the function will pick the best practice choice for you. The -Caching parameter supports Intellisense (Ctrl+Space or TAB completion).

  • While a disk has been added you will see the progress with some info about the new disk.
    04.New-AzVmDisk_WriteProgress

VMSize restrictions

  • Here we tried to add third data disk, but VMSize restriction did not allow doing that.

05.New-AzVmDisk_ErrorVMSizeRestriction

  • Have you forgotten about Get-AzVmDisk? One of the returned properties for each disk is the parent VM’s VMSize.

06.Get-AzVmDisk_VMSize

  • The built-in Get-AzureRmVMSize cmdlet will help us to know the VMSize disk related restrictions (MaxDataDiskCount property). The VMSizes may differ depending on Location.
Get-AzureRmVMSize -Location ((Get-AzureRmResourceGroup –Name $AzResourceGroup).Location) | select Name, max* | sort Name | ft –au
  • You can group the VMSize by MaxDataDiskCount property. This code seems ugly, but his output will give you all VMSizes that meet your VM disk requirements.
Get-AzureRmVMSize -Location ((Get-AzureRmResourceGroup -Name $AzResourceGroup).Location) | select Name, MaxDataDiskCount | sort MaxDataDiskCount | group MaxDataDiskCount | select @{ N = 'MaxDataDiskCount'; E = { $_.Name } }, Count, @{ N = 'VMSize'; E = { ($_ | select -expand Group | sort Name).Name -join ', ' } } | ft -au –Wrap

07.Get-AzureRmVMSize_MaxDataDiskCount

–StorageAccount parameter

  • The New-AzVmDisk supports –StorageAccount parameter. This parameter can be one of the three options (OSDisk, FirstDataDisk or Prompt). The Intellisense (Ctrl+Space or TAB completion) is supported.

–StorageAccount Prompt

  • Option Prompt will show you the list of all StorageAccounts that are in your VM’s ResourceGroup. This menu generated by Write-Menu function, included in the module.
    08.New-AzVmDisk_StorageAccountPrompt

–StorageAccount FirstDataDisk

  • Option FirstDataDisk will place your newly created data disk on the same StorageAccount with very first data disk already attached to your VM. If no one data disk exists, the new disk will be placed on the system disk’s StorageAccount.
    09.New-AzVmDisk_StorageAccountFirstDataDisk

–StorageAccount OSDisk

  • The last option OSDisk is self-explained.

Container property

  • Note regarding the Container property, returned by Get-AzVmDisk. No matter what is its name in your environment, but the New-AzVmDisk assumes it exists on all StorageAccounts where you are planning to deploy new data disks!

  • The built-in Get-AzureStorageContainer cmdlet will help you to discover all your StorageAccounts.

Get-AzureRmStorageAccount | Get-AzureStorageContainer | select @{N='StorageAccount';E={$_.Context.StorageAccountName}}, @{N='Container';E={$_.Name}} | ? {$_.Container -notmatch '^bootdiag'} | sort StorageAccountName, Container
  • The following one-liner will find all your StorageAccounts that contain Container, named vhds.
Get-AzureRmStorageAccount | Get-AzureStorageContainer | select @{N='StorageAccount';E={$_.Context.StorageAccountName}}, @{N='Container';E={$_.Name}} | ? {$_.Container -eq 'vhds'} | sort StorageAccountName

10.Get-AzureStorageContainer

Summary

  • Both functions can operate with multiple VM. In the case of New-AzVmDisk, a single data disk with the same settings (caching mode and disk size) will be added to all passed VMs. What may vary from VM to VM is a disk placement, controlled by -StorageAccount parameter.
Get-AzureRmVM -ResourceGroupName $AzResourceGroup | New-AzVmDisk –StorageAccount FirstDataDisk
Get-AzureRmVM -ResourceGroupName $AzResourceGroup | New-AzVmDisk -SizeGB 100 -Caching ReadWrite–StorageAccount Prompt
  • For more details about the function, please take a look at the content based help and examples.
Get-Alias -Definition New-AzVmDisk
Get-Help New-AzVmDisk -Full
Get-Help Get-AzVmDisk -Examples
Get-Help New-AzVmDisk -Parameter SizeGB

You may also like:

How to find orphaned VHD files in the Azure
How to login to the Azure automatically
Azure VM Tag automation
Azure Automation PowerShell Az-Module Home

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s