Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

Convert Azure VMs to Managed Disks using PowerShell

·581 words·3 mins· 100 views · 5 likes ·
Azure PowerShell Connect-AzAccount ConvertTo-AzVMManagedDisk Get-AzAvailabilitySet

This post will show you how to convert a virtual machine, whether within an availability group or a single instance, from unmanaged disks (Storage Account) to disks managed using Azure PowerShell. Requirements: This tutorial assumes that you already have a Microsoft Azure account set up. Important: Before starting the conversion process, you should keep in mind the following.

  • The conversion requires a restart of the VM
  • The conversion is not reversible.
  • The virtual machine will receive a new IP address after the conversion if it uses dynamic IP.
  • The VHDs and the storage account used by the VM before conversion are not deleted.
Azure PowerShell Workaround>

Azure PowerShell Workaround #

If you want to know how to install the PowerShell Azure module on your machine, check out this link. The simplest way to get started is to sign in interactively at the command line.

Connect-AzAccount

This cmdlet will bring up a dialog box prompting you for your email address and password associated with your Azure account. If you have more than one subscription associated with your mail account, you can choose the default subscription. To perform this task, we will use the following commands:

Get-AzSubscription
Select-AzSubscription -Subscription "My Subscription"
Convert a single VM from Unmanaged disk to Managed disk>

Convert a single VM from Unmanaged disk to Managed disk #

Set the variables Here, we define the characteristics of our environment

$resourceGroupName = "RG-DEMO"
$vmName = "VM-DEMO"
Check what type of disk the VM uses>

Check what type of disk the VM uses #

To verify that the machine does not use managed disks, use Get-AzVM with the following syntax:

(Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName).StorageProfile.OsDisk

If the value of the “ManagedDisk” property is empty, the machine uses unmanaged disks.

Get-AzVM

Deallocate the Virtual Machine>

Deallocate the Virtual Machine #

To deallocate the VM, use the Stop-AzVM cmdlet with the following syntax:

Stop-AzVM `
  -ResourceGroupName $resourceGroupName `
  -Name $vmName `
  -Force
Convert the VM to managed disks>

Convert the VM to managed disks #

To convert all the disks of the VM (os disk and data disk) to managed disks, use the ConvertTo-AzVMManagedDisk cmdlet with the following syntax:

ConvertTo-AzVMManagedDisk `
  -ResourceGroupName $resourceGroupName `
  -VMName $vmName

ConvertTo-AzVMManagedDisk
When verifying again with the Get-AzVM cmdlet, we see that the “ManagedDisk” property is not empty.

Azure Managed Disk
>

Azure Managed Disk
#

Convert VMs in an availability set to managed disks>

Convert VMs in an availability set to managed disks #

Set the variables Here, we define the characteristics of our environment

$resourceGroupName = "RG-DEMO"
$vmName = "VM-DEMO"
$availabilitySetName = "AV-DEMO"
Convert the availability set to a managed availability set>

Convert the availability set to a managed availability set #

Before converting virtual machines, you must first convert the availability set to a managed availability set. To do this, use the Update-AzAvailabilitySet cmdlet with the following syntax:

$avSet = Get-AzAvailabilitySet `
  -ResourceGroupName $resourceGroupName `
  -Name $availabilitySetName
Update-AzAvailabilitySet `
  -AvailabilitySet $avSet `
  -Sku Aligned
Update-AzAvailabilitySet
>

Update-AzAvailabilitySet
#

Deallocate and convert the VMs in the availability set>

Deallocate and convert the VMs in the availability set #

With the following script, we go through all the virtual machines within the availability group. First, we deallocate them with the Stop-AZM cmdlet and then convert them with the ConvertTo-AzVMManagedDisk cmdlet.

foreach($vmInfo in $avSet.VirtualMachinesReferences)
{
  $vm = Get-AzVM `
    -ResourceGroupName $resourceGroupName `
    | Where-Object {$_.Id -eq $vmInfo.id}
  Stop-AzVM `
    -ResourceGroupName $resourceGroupName `
    -Name $vm.Name `
    -Force
  ConvertTo-AzVMManagedDisk `
    -ResourceGroupName $resourceGroupName `
    -VMName $vm.Name
}

Azure Managed Disk
If you want to know more about Azure Managed Disks, check out this link.