Traditionally, Azure Virtual Machines (VMs) would use an Azure Storage Account to store the VM Disk Images for the VM. This required explicit management of what Storage Accounts contained which and how many VM Disks; both OS Disks and Data Disks. To simplify the management of the VM Disks, Microsoft released a newer feature (that is now the recommended best practice) called Azure Managed Disks. Managed Disks allow you to store the VM Disk imaged (both OS and Data disks) in Azure without the need to manage what Storage Accounts are used. You simple create Managed Disks, and the Azure platform takes care of all the management and scalability necessary.

When creating a Virtual Machine in Microsoft Azure, you can choose whether to use Managed Disks or not. Even though this can be chosen now at creating time, you may still have a number of Virtual Machines that are not using Managed Disks. Thankfully, Microsoft has provided tooling within the Azure CLI that enables you to easily Convert a VM to use Managed Disks.

Let’s take a look at this below…

Convert Single VM to use Managed Disks

First, to convert an Azure Virtual Machine to Managed Disks, you need to deallocate the VM, perform the conversion, then start the VM back up again. You’ll need to be sure to take care when performing this on VMs in a Production environment.

Here’s the commands necessary to do this:

# deallocate the VM
az vm deallocate --resource-group myGroup --name myVM

# Convert VM to Managed Disks
az vm convert --resource-group myGroup --name myVM

# Start the VM back up again
az vm start --resource-group myGroup --name myVM

This script works with a single VM. Let take a look at converting VMs in an Availability Set.

Convert an Availability Set to use Managed Disks

With Virtual Machines in an Availability Set, first the Availability Set need to be converted to a Managed Availability Set. Once this has been done, then the individual VMs within the Availability Set can be converted.

Here’s the commands necessary to convert an Availability Set to a Managed Availability Set:

# Deallocate all the VMs within the Availability Set
# Run this command for each VM in the Availability Set
az vm deallocate --resource-group myGroup --name myVM

# Convert Availability Set to be Managed
az vm availability-set convert \
    --resource-group myGroup \
    --name myAvailabilitySet

# Convert all the VMs to Managed Disks
# Run this command for each VM in the Availability Set
az vm convert --resource-group myGroup --name myVM

# Start all the VMs back up again
# Run this command for each VM in the Availability Set
az vm start --resource-group myGroup --name myVM

To easily locate all the VMs within the Availability Set, you can run the following command:

az vm availability-set show \
    --resource-group myGroup \
    --name myAvailabilitySet \
    --query [virtualMachines[*].id] \
    --output table

Convert Multiple VMs at Once

You can also use some query techniques to run these commands against multiple VMs using a loop within the Bash script. This technique is outlined in a previous article titled “Azure CLI 2.0: Quickly Start / Stop ALL VMs“.

# deallocate ALL VMs in Availability set at once
az vm deallocate --ids \
    $(az vm availability-set show \
    --resource-group myGroup \
    --name myAvailabilitySet \
    --query "virtualMachines[*].id" \
    --output tsv)

# Convert Availability Set to be Managed
az vm availability-set convert \
    --resource-group myGroup \
    --name myAvailabilitySet
# convert ALL VMs at once
az vm convert --ids \
 $(az vm availability-set show \
 --resource-group myGroup \
 --name myAvailabilitySet \
 --query "virtualMachines[*].id" \
 --output tsv)

# start ALL VMs back up again
az vm start --ids \
 $(az vm availability-set show \
 --resource-group myGroup \
 --name myAvailabilitySet \
 --query "virtualMachines[*].id" \
 --output tsv)

Have fun scripting!

Posted by Chris Pietschmann

Chris is a Microsoft MVP and has nearly 20 years of experience building enterprise systems both in the cloud and on-premises. He is also a Certified Microsoft Azure Solutions Architect (both MCSD and MCSE), a Microsoft Certified Trainer, and self proclaimed Cloud Advocate. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.

One Comment

  1. Nice! However, after Managed vs Unmanaged comparison I’m not sure if I’d like to migrate to managed VMs (especially knowing that there is no simple way back)

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.