Restore Azure VM from Snapshot

How to restore an Azure VM from a snapshot

#Provide the name of your resource group 
$resourceGroupName ='yourResourceGroupName' 

#Provide the name of the snapshot that will be used to create OS disk 
$snapshotName = 'yourSnapshotName' 

#Provide the name of the OS disk that will be created using the snapshot 
$osDiskName = 'yourOSDiskName'

$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 -DiskName $osDiskName 

# Get the VM  
$vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name myVM  

# Make sure the VM is stopped\deallocated 
Stop-AzVM -ResourceGroupName $resourceGroupName -Name $vm.Name -Force 

# 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 $resourceGroupName -VM $vm  

# Start the VM 
Start-AzVM -Name $vm.Name -ResourceGroupName $resourceGroupName

References

https://docs.microsoft.com/en-us/azure/virtual-machines/scripts/virtual-machines-windows-powershell-sample-create-vm-from-snapshot

https://docs.microsoft.com/en-us/azure/virtual-machines/windows/os-disk-swap

Leave a Reply

Your email address will not be published. Required fields are marked *