Azure: Set-AzVMOSDisk doesn't replace the OsDisk, but also doesn't show error

Dan Black 26 Reputation points
2021-12-16T04:55:26.077+00:00

I'm trying to restore a VM OsDisk back to a previously created Snapshot.

Here's my ps1 file:

Param($resourceGroupName, $vmName, $snapshotName)

$snapshot = Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName 

$diskConfig = New-AzDiskConfig -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy

$disk = New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -Name "$($vmName)_$([guid]::NewGuid())"

$vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName

$oldDiskName = $vm.StorageProfile.OsDisk.Name

$virtualMachine = Set-AzVMOsDisk -VM $vm -ManagedDiskId $disk.Id -CreateOption Attach -Windows

Remove-AzDisk -ResourceGroupName $resourceGroupName -Name $oldDiskName -Force

The final step, to remove the old OsDisk, is throwing an error:

Disk <diskname> is attached to VM

Which means the old disk is still attached to the VM, which means that Set-AzVMOsDisk didn't work.

Any ideas?

Thanks

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,044 questions
{count} votes

Accepted answer
  1. shiva patpi 13,366 Reputation points Microsoft Employee Moderator
    2021-12-17T22:43:59.487+00:00

    Hello @Dan Black ,
    Glad that you are able to figure it out. Yes - That's the right approach.
    By using Set-AzVMOSDisk you are modifying the state of the Virtual machine by using it's attributes and those changes are cached.
    Now, in order for those changes to reflect - it needs to be followed by either Update-azVM / New-AzVM

    Couple of examples:
    https://learn.microsoft.com/en-us/powershell/module/az.compute/set-azvmdatadisk?view=azps-7.0.0

    Other easy way of updating is: (You can just use pipe symbol followed by update-azvm)
    Set-AzVMOSDisk -VM $azureVM -ManagedDiskId $newVMDisk.Id -Name $newVMDiskName | update-azvm

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Dan Black 26 Reputation points
    2021-12-16T18:20:09.367+00:00

    I got some help:

    Set-AzVMOsDisk just stages the changes, but you then need to use Update-AzVM to apply the changes back to Azure like this:

    Param(
        $resourceGroupName,
        $vmName,
        $snapshotName
    )
    
    # Get target VM and snapshot
    
    $azureVM = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
    
    $snapshot = Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName
    
    
    
    # Create new disk config and provision managed disk
    
    $diskConfig = New-AzDiskConfig -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption 'Copy'
    
    $oldVMDisk = $azureVM.StorageProfile.OsDisk.Name
    
    $newVMDiskName = "$($vmName)_OsDisk_$([guid]::NewGuid())"
    
    $newVMDisk = New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -Name $newVMDiskName 
    
    
    
    # Set new OS managed disk (pending VM config)
    
    Set-AzVMOSDisk -VM $azureVM -ManagedDiskId $newVMDisk.Id -Name $newVMDiskName 
    
    # Apply pending VM config
    
    Update-AzVM -ResourceGroupName $resourceGroupName -VM $azureVM
    
    # Delete old OS disk
    
    Remove-AzDisk -ResourceGroupName $resourceGroupName -Name $oldVMDisk -Force
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.