Script Sample - VM Operations - Convert VM to Managed Disk VM

@20aman    Dec 02, 2018

If you want to convert multiple VMs from older Storage Account disks to Managed Disks you can leverage the automation capabilities provided by Microsoft via PowerShell. This script sample uses these capabilities in a managed and reusable way.

Script Requirements

The script requires you to populate the configurations CSV file. The sample CSV file is also provided along with the script. This configurations file should have the following columns:

  1. Computer - Name of the VM in Azure
  2. OSType - Windows or Linux
  3. ResourceGroupName - Resource Group of the VM
  4. ManagedDiskConversion - yes or no. Set this to yes for the VMs for which you want to perform the conversion from older disk type to Managed Disk VMs

Script Working

The process of Managed Disk conversion is different for a VM with Availability Set and one without it. For this reason the script first checks for the Availability Set of the VM.

$avSet = Get-AzureRmAvailabilitySet -ResourceGroupName $rgName -Name $asName -ErrorAction Stop

It then checks and sets the Managed flag on the Availability Set by using the following command.

Update-AzureRmAvailabilitySet -AvailabilitySet $avSet -Managed -ErrorAction Stop

It then stops the VM, converts it to Managed Disk Vm and then waits for 600 seconds. The conversion is performed by following command.

ConvertTo-AzureRmVMManagedDisk -ResourceGroupName $rgName -VMName $vm.Name

Note: The script does not start the VM to avoid unnecessary costs. E.g. if the VM was stopped to begin with and you didn't want to start it. Unnecessary starting of a VM will incur you charges that you do not want. You can still update the script to start the VMs by simply adding the cmdlet for Start-AzureRmVM if you want.

The script encapsulates all this logic into a reusable function. It also provides the best practices template to invoke this function. Note: Check the comments marked with "ToDo" where you should be making the changes.

Location of the Script

You can find this script in GitHub at this location: Convert-VMToManagedDisk.ps1





Comments powered by Disqus