Swap OS Disk of an Azure VM

Have you ever been in a situation where you did the troubleshooting for a VM by attaching its OS Disk as Data Disk to other Azure VM? Then created Azure VM out of that OS Disk. It’s a painful process. What if we can swap the existing OS Disk of an Azure VM? So you don’t have to delete and recreate VM.

Well, there is an option to do that. It’s been here for a quite long time with the unmanaged disk, and it’s available for Azure Managed Disk as well. There are multiple ways to do the same. We will focus on the Azure Portal and PowerShell.

To swap the OS Disk, follow the below instructions on the Azure Portal:

  1. Go to the Azure VM of which you want to replace the OS Disk
  2. Stop the VM
  3. Select Disks from the bladeazure vm, azure managed disk, azure virtual machine
  4. Click on Swap OS Disk option
  5. Choose the disk you want to replace with and provide the name of the VMazure vm, azure managed disk, azure virtual machine
  6. Click OK after filling above entries
  7. Once the swapping is complete, you can start the VM

If you prefer PowerShell use below cmdlets to Swap the OS Disk:

# Get the VM
$vm = Get-AzVM -ResourceGroupName myResourceGroup -Name myVM
# Make sure the VM is stopped\deallocated Stop-AzVM -ResourceGroupName myResourceGroup -Name $vm.Name -Force
# Get the new disk that you want to swap in $disk = Get-AzDisk -ResourceGroupName myResourceGroup -Name newDisk
# Set the VM configuration to point to the new disk Set-AzVMOSDisk -VM $vm -ManagedDiskId $disk.Id -Name $disk.Name
# Update the VM with the new OS disk Update-AzVM -ResourceGroupName myResourceGroup -VM $vm
# Start the VM Start-AzVM -Name $vm.Name -ResourceGroupName myResourceGroup

I believe this process will make your VM troubleshooting easy.

Leave a Reply