Add multiple managed disks to Azure RM VM
In this post I have created a PowerShell script to help add multiple managed disks to an Azure RM Virtual Machine.
The script to add multiple managed disks will prompt you to login to an Azure RM account, then it will query the subscriptions and ask you to select the desired. After that it will query the available VMs and promt to select the target VM from the VM list.
At this point I am checking the OS disk and define the storage type of the data disk. If we need to change the storage type we can check the comments at step 4. e.g. If the OS disk is Premium and you want Standard data disks.
The next step is to ask for disk size. You can check the sizes and billing here: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/managed-disks-overview#pricing-and-billing
Finally it will ask for the number of the disk we need to create. After this input the script will create the disks, attach them to the VM and update it.
The Script:
# 1. You need to login to the Azure Rm Account Login-AzureRmAccount # 2. The script will query the Subscriptions that the login account has access and will promt the user to select the target Subscription from the drop down list $subscription = Get-AzureRmSubscription | Out-GridView -Title "Select a Subscription" -PassThru Select-AzureRmSubscription -SubscriptionId $subscription.Id # 3. The script will query the available VMs and promt to select the target VM from the VM list $vm = Get-AzureRmVM | Out-GridView -Title "Select the Virtual Machine to add Data Disks to" -PassThru # 4. I set the storage type based on the OS disk. If you want to spesify somehting else you can cahnge this to: $storageType = StandardLRS or PremiumLRS etc. $storageType = $VM.StorageProfile.OsDisk.ManagedDisk.StorageAccountType # 5. The script will promt for disk size, in GB $diskSizeinGB = Read-Host "Enter Size for each Data Disk in GB" $diskConfig = New-AzureRmDiskConfig -AccountType $storageType -Location $vm.Location -CreateOption Empty -DiskSizeGB $diskSizeinGB # 6. Enter how many data disks you need to create $diskquantity = Read-Host "How many disks you need to create?" for($i = 1; $i -le $diskquantity; $i++) { $diskName = $vm.Name + "-DataDisk-" + $i.ToString() $DataDisk = New-AzureRmDisk -DiskName $diskName -Disk $diskConfig -ResourceGroupName $vm.ResourceGroupName $lun = $i - 1 Add-AzureRmVMDataDisk -VM $vm -Name $DiskName -CreateOption Attach -ManagedDiskId $DataDisk.Id -Lun $lun } Update-AzureRmVM -VM $vm -ResourceGroupName $vm.ResourceGroupName
You can download the script from here: AddManagedDisks
Pantelis Apostolidis is a Sr. Specialist, Azure at Microsoft and a former Microsoft Azure MVP. For the last 20 years, Pantelis has been involved to major cloud projects in Greece and abroad, helping companies to adopt and deploy cloud technologies, driving business value. He is entitled to a lot of Microsoft Expert Certifications, demonstrating his proven experience in delivering high quality solutions. He is an author, blogger and he is acting as a spokesperson for conferences, workshops and webinars. He is also an active member of several communities as a moderator in azureheads.gr and autoexec.gr. Follow him on Twitter @papostolidis.
Hi ,
This script is very cool and helps me to add disk multiple disks at one shot. But this script doesn’t work if you wanted to add additional disks later to the same vm. Basically, this won’t add the disk based on free Lun. It would be great if you can add some function to check and add.
Hi Logan
thank you for your feedback!! As we discussed I will try to change the script in order to first check for available LUNs and then add the disks.
Script Version 2:
Changes:
Shows the disk capacity of the VM and asks how many disks to add.
Prompts for the size of every disk
Checks for empty Luns to add the disks
http://ge08jfh8ds93sdf.azurewebsites.net/microsoft/azure/azure-vm-add-multiple-data-disks-v2/
https://github.com/proximagr/automation/blob/master/Add-DataDisks-DIffSize_v3.ps1