Automation | VM Disk Attachment


Disk_attach.ps1 is a PowerShell script to attach data disks for all VMs provided in the input csv.

The following guide assumes that the user has basic knowledge of using Windows and PowerShell.

Although some familiarity with the Azure Portal would be beneficial, it is absolutely not necessary, as a step by step approach will be provided.

Script workflow:

  • Collects all the input from csv
  • Select subscription
  • Validate VM name exist in the subscription if does exists then skips that particular VM during execution.
  • Add data disks to validated VMs
  • Restart the VM.
  1. Pre-requisites

The following prerequisites must be met, before performing the Core capacity check steps.

  1. Internet connection
  2. Valid Azure Subscription
  3. The person who will perform the below steps must have one of the following roles in the Azure subscription
    • Owner
    • Contributor
  4. Some familiarity with the Azure Portal would be beneficial, although not necessary
  5. Latest version of Microsoft .NET Framework (download link)
  6. Latest version of Microsoft Windows Management Framework (download link)
  7. Latest version of Microsoft PowerShell (download link including detailed installation guide)
  8. Latest version of Azure cmdlets (installation guide), although steps are provided below
  9. Some familiarity with PowerShell ISE, although not necessary (documentation)
  10. Some familiarity with the Azure Portal would be beneficial, although not necessary
  11. Some familiarity with PowerShell scripting would be beneficial, although not necessary
  12. Disk Attach Script

Windows PowerShell scripts can also be used for automating the VM data disk addition process. It minimizes the human efforts and errors. It can be used to attach multiple data disk to multiple VM’s in different subscription, therefore saving a lot of time. Windows Azure PowerShell module should be installed to use azure based commend lets (as mentioned in pre requisites).

Follow the following link to download and configure the module –

  • For using this script,
    1. Open PowerShell ISE and type below command

# connecting your azure account to powershell


It gives the following popup

    1. Enter your credentials and click sign in. Once your account is added, just open the CSV file attached with script you downloaded.
    2. Add the VM details as required and save the file before closing it

Note: Data Disks “Host catching” should be set to None unless the client specifically requested for “Read only or Read/Write”, if required change the variable in the script.

  1. Now go back to the PowerShell and execute the script
  2. Once the script completes, please login to the server and perform the Disk initiation at the OS Layer and assign the proper drive letters.

The script is listed below which need to be executed.

######Importing the Csv file##########
$datadrive = Import-Csv -path “C:\Users\AzureAdmin\Desktop\disk_attach\DiskAttach.csv” #Give the path of input Csv file.

foreach($data in $datadrive)
###fetching data from CSV###
$ResourceGroupName = $data.ResourceGroupName
$StorageAccName = $data.StorageAccName
$SubscriptionName = $data.SubscriptionName
$ServerName = $data.ServerName
$ErrorActionPreference = “Stop”
Select-AzureRmSubscription -SubscriptionName $SubscriptionName | Out-Null #Selecting the azure subscription
$lun = 0

###checking for logical unit number(LUN)###
$Vm = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $ServerName
if($vm.StorageProfile.DataDisks -ne $null){$lun = $vm.StorageProfile.DataDisks.lun[-1] + 1}

###stopping VM###
Write-Verbose -Verbose “Stopping VM $ServerName ”
Stop-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $ServerName -force

###Attaching datadisk to VM###
for($i=0;$i -lt 10;$i++)
$DiskName = $data.”diskName$i”
$DiskSize = $data.”diskSize$i”
if(($DiskName -eq $null) -or ($DiskName -eq “”)){}
$DriveName = $ServerName + “-” + $DiskName
$Vm = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $ServerName
$vhdURI = “https://” + $StorageAccName + “” + $ServerName + “-” + $DiskName + “.vhd”
$a = Add-AzureRmVMDataDisk -VM $Vm -Name $DriveName -DiskSizeInGB $DiskSize -VhdUri $vhdURI -Caching $hostcaching -CreateOption empty -Lun $lun
Write-Verbose -Verbose “Attaching disk $DiskName to $ServerName ”
Update-AzureRmVM -ResourceGroupName $ResourceGroupName -VM $Vm
write-verbose -Verbose “Disk attached”
if($_.exception.message -like “*Long running operation failed with status*”){}
write-Host $_.Exception.Message -ForegroundColor Yellow
###starting VM###
Write-Verbose -Verbose “Starting VM $ServerName ”
Start-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $ServerName

Please download the input file from DiskAttach.csv

Leave a Reply

Your email address will not be published.

Close Bitnami banner