/ Managed Disk

Creating and Restoring Azure Virtual Machine Snapshots for Managed Disks

At the moment official way to restore Virtual Machine from a snapshot is to recreate VM. Why not just replace OS disk? Let's start from the beginning.

Shutdown Virutal Machine

$resourceGroupName = "TestGroup"
Stop-AzureRmVM -ResourceGroupName $resourceGroupName -Name "VMtoRestore"

Create snapshot

$location = 'WestEurope'

$Disk = Get-AzureRmDisk -ResourceGroupName $resourceGroupName -Name "osdisk"
$SnapshotName = ($Disk.Name + '_snap')

$Snapshot = New-AzureRmSnapshotConfig -SourceUri $Disk.Id -CreateOption Copy -Location $location
New-AzureRmSnapshot -Snapshot $Snapshot -SnapshotName $SnapshotName -ResourceGroupName $ResourceGroupName

Create Managed Disk from Snapshot

$StorageType = "PremiumLRS" # if you need hdd use StandardLRS

$Snapshot = Get-AzureRmSnapshot -ResourceGroupName $ResourceGroupName -SnapshotName $SnapshotName 

$DiskConfig = New-AzureRmDiskConfig -AccountType $StorageType -Location $location -CreateOption Copy -SourceResourceId $Snapshot.Id

$NewDiskName = ($Disk.Name + '_newdisk')
New-AzureRmDisk -Disk $DiskConfig -ResourceGroupName $ResourceGroupName -DiskName $NewDiskName

Remove-AzureRmSnapshot -ResourceGroupName $ResourceGroupName -SnapshotName $SnapshotName -Force # you have disk so you dont need snapshot

Replace OS disk on VM

Now we need just "redeploy" our VM.

$VM = Get-AzureRmVM -ResourceGroupName $resourceGroupName -Name "VMtoRestore"
$NewDisk = Get-AzureRmDisk -ResourceGroupName $resourceGroupName | Where-Object {$_.name -like $NewDiskName} | Select-Object name, id

$VM.StorageProfile.OsDisk.Name = $NewDisk.Name
$VM.StorageProfile.OsDisk.ManagedDisk.id =  $NewDisk.Id

Update-AzureRmVM -VM $VM -ResourceGroupName $resourceGroupName