powershellazureazure-powershellazure-managed-disk

Restore managed OS disk snapshot to existing VM


Similar to this one but powershell exclusively and with managed disks and an existing VM.

I took a snapshot of a managed OS disk and want to restore it but can't figure out how. I have tried a number of things but now think that you can't detach an OS disk even if the VM is deallocated. No matter how much I pore through the reference, I can't find anything to help me restore a snapshot to an existing disk. Is this even possible?


Solution

  • I can't find anything to help me restore a snapshot to an existing disk. Is this even possible?

    As far as I know, Azure does not support restore a snapshot to an existing disk.

    But we can use the snapshot to create a Managed Disk and attach it to an existing VM.

    Here is the PowerShell script use snapshot to create a Managed Disk:

    PS C:\Users> $resourceGroupName = 'vm'
    PS C:\Users> $snapshotResourceGroupName = 'vm'
    PS C:\Users> $snapshotName = 'manageddisk1'
    PS C:\Users> $managedDiskType = 'StandardLRS'
    PS C:\Users> $location = 'eastus'
    PS C:\Users> $managedDiskCreateOption = 'Copy'
    PS C:\Users> $diskName = 'manageddisk2'
    PS C:\Users> $snapshot = Get-AzureRmSnapshot -SnapshotName $snapshotName -ResourceGroupName $snapshotResourceGr
    oupName
    PS C:\Users> $diskConfig = New-AzureRmDiskConfig -AccountType $managedDiskType -Location $location -CreateOptio
    n $managedDiskCreateOption -SourceResourceId $snapshot.Id
    PS C:\Users> New-AzureRmDisk -DiskName $diskName -Disk $diskConfig -ResourceGroupName $resourceGroupName
    
    
    AccountType        : StandardLRS
    TimeCreated        : 4/21/2017 1:26:27 PM
    OsType             : Windows
    CreationData       : Microsoft.Azure.Management.Compute.Models.CreationData
    DiskSizeGB         : 128
    EncryptionSettings :
    OwnerId            :
    ProvisioningState  : Succeeded
    Id                 : /subscriptions/5384xxxx-xxxx-xxxx-xxxx-xxxxe29axxxx/resourceGroups/vm/providers/Microsoft.Compute/
                         disks/manageddisk2
    Name               : manageddisk2
    Type               : Microsoft.Compute/disks
    Location           : eastus
    Tags               :
    

    If you want to attach it to an existing VM, we can use this script:

    PS C:\Users> $datadisk2 = Get-AzureRmDisk -ResourceGroupName vm -DiskName manageddisk2
    PS C:\Users> $vmName = 'jasonvm'
    PS C:\Users> $rgname = 'vm'
    PS C:\Users> $dataDiskName = 'manageddisk2'
    PS C:\Users> $vm = Get-AzureRmVM -Name $vmName -ResourceGroupName $rgName
    PS C:\Users> $vm = Add-AzureRmVMDataDisk -VM $vm -Name $dataDiskName -CreateOption Attach -ManagedDiskId $dataD
    isk2.Id -Lun 2
    PS C:\Users> Update-AzureRmVM -VM $vm -ResourceGroupName $rgName
    
    RequestId IsSuccessStatusCode StatusCode ReasonPhrase
    --------- ------------------- ---------- ------------
                             True         OK OK
    

    In this way, we can find this managed disk in Azure VM: enter image description here