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.
# 1. You need to login to the Azure Rm Account
# 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
Azure Managed Disks | Easy Scale, High Available, Secure
Azure Managed Disks is almost five months old – Start using it, it’s simple, easy to scale, high available and secure. As Microsoft says, “Let Azure takes care of your disks”.
The idea is simple, choose the performance tier and the size you want. After that, you are free to change your mind! You can change the performance tier (yes, switch between SSD & HDD) and the size with just a click.
Let’s get it started. First of all, we need to enable the managed disks at the VM creation. Specifically, at the third step select “Yes” at the “Use managed disks” setting. After that, you don’t have to worry about storage account, Azure takes care of this.
Once the VM is deployed, go to the VM’s blade and click “Disks” and “+ Add data disk”
After that, the “Create managed disk” blade opens and there are some interesting settings to choose. Let’s get a closer look.
First, choose a name for the Data Disk, choose a Resource Group and Account type. This is the performance tier, SSD or HDD.
Next, at the Source type drop down menu, we can choose to create an empty disk, by selecting the None. Also, we can select to use a Snapshot that we have already created or a Storage blob to select a disk.
After that it is the size. The Managed Disks have specific price, IOPS and price. Type the Size in Gigabytes and see the estimated performance instantly.
You can find the details here: https://azure.microsoft.com/en-us/pricing/details/managed-disks/
Standard Managed Disks:
All standard managed disks have 500 IOPS and 60MB/sec throughput
Premium Managed Disks:
And what happens with the existing VMs that have “classic” unmanaged disk? No worries, just a bit of PowerShell and you can convert that to managed disks.
How to do it? First Stop the VM, not just show down, we need to Stop (Deallocate it). Then run just this line of PowerShell code:
ConvertTo-AzureRmVMManagedDisk -ResourceGroupName rgname -VMName vmname
Now some magic. SSD to HDD to SSD to HDD and go on!
Let’s say you have created a Standard disk, HDD, but now you need performance. Just go to the VM blade, first Stop the VM and then select the “Disks”, find the disk and change the “Account type”.
Just save, and voila! you have SSD, from 500 IOPS to 5000 IOPS! Any time, you can Stop the VM and change the disk back to Standard (HDD)
Some more magic? Click “Export” at the disk properties, set an expiration time and Generate URL. You need to Stop the VM first.
A PUBLIC URL is generated. You can use it to download the VHD without having to login to the Portal.
At the disk properties click “Crete snapshot”
Enter a Name, select Resource group and Account type
Now, under the Resources of the Resource Group you will find the Snapshot