Technical tidbits from the sysadmin world...

Data disks in Azure Resource Manager Powershell

The search for how to do some of what is below was one of the main reasons I started thinking about writing a technical blog… On to the interesting stuff!

First up – check your LUNs!

If you have already added data disks via the portal you should check what their current LUNs are to avoid any errors, run the following and look for the LUN tag in the disk output.

Note that the OS Disk is on a separate controller so ignore its LUN and only look for data disks!

Get-AzurermVM -ResourceGroupName AZMONKEY -Name AZMONKEYVM01


Add a new (empty) Data Disk to a VM

This is relatively straight forward, you specify how big you want the disk and set it to the empty creation option – one thing to keep in mind is if you are attaching multiple data disks you will need to specify the LUN number so there are no conflicts

#Add Data Disks - Suggest only adding same caching type at once and setup in Windows to avoid confusion 

#Specify your VM Name 
#Specify your Resource Group 
$rgName = "AZMONKEY" 
#Specify your Storage account name
#This pulls your storage account info for use later 
$storageAcc=Get-AzureRmStorageAccount -ResourceGroupName $rgName -Name $saName 
#Pulls the VM info for later 
$vmdiskadd=Get-AzurermVM -ResourceGroupName $rgname -Name $vmname 
#Sets the URL string for where to store your vhd files - converts to
#Also adds the VM name to the beginning of the file name 
$DataDiskUri=$storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/" + $vmName 
Add-AzureRmVMDataDisk -CreateOption empty -DiskSizeInGB 1023 -Name $vmname-Data01 -VhdUri $DataDiskUri-Data01.vhd -VM $vmdiskadd -Caching ReadOnly -lun 0 
Add-AzureRmVMDataDisk -CreateOption empty -DiskSizeInGB 1023 -Name $vmName-Data02 -VhdUri $DataDiskUri-Data02.vhd -VM $vmdiskadd -Caching ReadOnly -lun 1 
Add-AzureRmVMDataDisk -CreateOption empty -DiskSizeInGB 1023 -Name $vmName-Data03 -VhdUri $DataDiskUri-Data03.vhd -VM $vmdiskadd -Caching None -lun 2 
Add-AzureRmVMDataDisk -CreateOption empty -DiskSizeInGB 1023 -Name $vmName-Data04 -VhdUri $DataDiskUri-Data04.vhd -VM $vmdiskadd -Caching ReadWrite -lun 3 
#Updates the VM with the disk config - does not require a reboot 
Update-AzureRmVM -ResourceGroupName $rgname -VM $vmdiskadd


Re-attach an existing disk

So say you moved some data disk vhds from premium to standard storage or visa versa – you need to reattach those disks after you detached them… how do you do it? Well its not too hard with the right syntax!

#attach existing datadisks to VM
$rgName = "AZMONKEY"
$vmname = "AZMONKEYVM01"
$vmdiskadd=Get-AzurermVM -ResourceGroupName $rgname -Name $vmname

Add-AzureRMVMDataDisk -Name AZMONKEYVM01-Data03 -VM $vmdiskadd -VhdUri -LUN 2 -Caching None -CreateOption Attach -DiskSizeInGB 1023
Add-AzureRMVMDataDisk -Name AZMONKEYVM01-Data04 -VM $vmdiskadd -VhdUri -LUN 3 -Caching None -CreateOption Attach -DiskSizeInGB 1023
Update-AzureRmVM -ResourceGroupName $rgname -VM $vmdiskadd