Adding Disks to Azure VM with Powershell

While a lot of projects in Azure may be using SQLDB or SQLDW (aka Dedicated Pools) we do still have a lot of customers running SQL on a VM (aka IaaS).

One common task is setting up data disks. Typically we may need anything from 4-20 data disks and combine them with storage spaces to try and get the most throughput for table scan speed on a data warehouse. In this example we have four 512GB data disks which should give about 600 MB/Sec of throughput, assumign the VM limit can handle that.

In the next part I’ll post sample code to create the Storage Pools, Virtual disks and Volumes

Adding Disks for a SQLVM in Azure IaaS with Powershell
#
# Script to Create and Attach Disks to a VM
# History: 28/03/2021 Bob (Prodata), Upgraded to only required latest Azure Powershell Library
# Install-Module Az (Included in CloudShell by default)
#
$ErrorActionPreference = "Stop"

#SET These Variables as needed 
$ResourceGroupName ="bobedw_group"
$vmName="bobedw"
$SubscriptionName="My Sub"
$TenantId="xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxx"
$vmName="bobedw"        
$DiskSizeInGB=512       #Size of Disks in GB#
$Disks=4                #Number of Disks to Add #
$Caching="None"         #Caching Policy None|ReadOnly|ReadWrite # 
$StartingLUN =0         #Set this to zero for First Disks and max LUN for not new VM

#Authenticate to Azure if required (not in cloudshell)
if (!(Get-AzContext)) {
    Connect-AzAccount -TenantId $TenantId  -SubscriptionName $SubscriptionName
}

#Set Contect to correct Subsription for VM
Set-AzContext -Tenant $TenantId -SubscriptionName $SubscriptionName

#Get Azure VM Context. Will Fail if not found
$VM=Get-AzVM -ResourceGroupName $ResourceGroupName -Name  $vmName

#Create and Attach Disks in a Loop for required Number of Disks
for ($i=1; $i -le $Disks; $i++) {
   Add-AzVMDataDisk  -VM $VM -Name DataDisk$i -Caching $Caching -DiskSizeInGB $DiskSizeInGB  -Lun ($StartingLUN  + $i-1) -CreateOption "Empty" 
}
Update-AzVM -VM $VM -ResourceGroupName $ResourceGroupName


#Sample script to delete if rollback needed
#Note: Careful deleting disks as need to detach AND physically remove from resource group (or will still be billed)
#Remove-AzVMDataDisk -VM $VM  -Name TestDisk2
#Update-AzVM -VM $VM -ResourceGroupName $ResourceGroupName

Leave a Reply