Azure OS disk swap managed disks

Azure now has the feature to swap the OS disk of a VM. This means that it is much easier to restore a VM using an older snapshot of the VM without the need of deleting the VM and starting again. You will need to make sure that the VM size and storage type are compatible with the disk you would like to attach.

You can get a list of all of the disks that you have available you can run the following PowerShell. This will tell you what disks are available in the resource group you have selected.

 

Get-AzureRmDisk -ResourceGroupName myResourceGroup | Format-Table -Property Name

 

To swap managed disks the VM must be in a stopped/deallocated state. Once it is in this state, the disk will then be available to see the resource ID. This means that you will be able to select that disk for use with another VM. To apply the disks to a VM you will need to run the following PowerShell commands.

 

# Get the VM

$vm = Get-AzureRmVM -ResourceGroupName myResourceGroup -Name myVM

 

# Make sure the VM is stopped\deallocated

Stop-AzureRmVM -ResourceGroupName myResourceGroup -Name $vm.Name -Force

 

# Get the new disk that you want to swap in

$disk = Get-AzureRmDisk -ResourceGroupName myResourceGroup -Name newDisk

 

# Set the VM configuration to point to the new disk 

Set-AzureRmVMOSDisk -VM $vm -ManagedDiskId $disk.Id -Name $disk.Name

 

# Update the VM with the new OS disk

Update-AzureRmVM -ResourceGroupName myResourceGroup -VM $vm

 

# Start the VM

Start-AzureRmVM -Name $vm.Name -ResourceGroupName myResourceGroup

 

When you start the VM at after changing disk it will use the new disk and snapshot that you have added in azure.

About the author