Latest posts by Adam Bertram (see all)
- Create a certificate-signed RDP shortcut via Group Policy - Fri, Aug 9 2019
- Monitor web server uptime with a PowerShell script - Tue, Aug 6 2019
- How to build a PowerShell inventory script for Windows Servers - Fri, Aug 2 2019
In PowerShell, Azure disk snapshots are possible using the New-AzSnapshot command. This command takes a snapshot of a virtual hard disk (VHD). You can also "restore" this snapshot by creating a new VM in Azure, but most of the time when you need to restore a snapshot, you'd prefer to use the same VM just like a Hyper-V checkpoint.
Luckily, this snapshot or checkpoint functionality is possible to some degree with the OS disk on Azure VMs using a little bit of PowerShell.
To create, restore, and manage these snapshots, I've created a PowerShell module called AzVmSnapshots. This module implements much of the code to create and restore Azure snapshots but also can attach snapshots to existing VMs, removing the requirement to create a new one.
To use the AzVmSnapshots module, you'll need to install the Az module via Install-Module -Name Az. After installation, you'll then need to connect to your Azure subscription using Connect-AzAccount. When authenticated, download the AzVmSnapshots module by running Install-Module AzVmSnapshots.
After the download, check out the commands available in this module.
You'll see we've got a few options. The New-AzVmSnapshot command creates a VHD snapshot for a particular VM. The Get-AzVmSnapshot command returns snapshots created with New-AzVmSnapshot, and Restore-AzVmSnapshot applies a saved snapshot to a specific VM.
To create a new snapshot, you can pass the VM name and resource group the VM exists in to the New-AzVmSnapshot command. This will stop the VM, create the VHD snapshot attached to the VM, and bring the VM back up.
New-AzVmSnapshot -VmName XXXX -ResourceGroupName 'XXXXXX'
When the snapshot is complete, you can then run Get-AzVmSnapshot to query all the snapshots you've created using the AzVmSnapshots module. New‑AzVmSnapshot creates VHD snapshots using the format AzVmSnapshot--. It applies this naming convention to differentiate snapshots taken with the AzVMSnapshots module from other snapshots you may have in your subscription.
Get-AzVmSnapshot -ResourceGroupName 'XXXXXXXXXXX'
Let's say you've now made some changes to your VM and you want to restore it to the same state as when you created the snapshot. We'll use Restore-AzVmSnapshot. To use this command, we can pipe the output of Get-AzVmSnapshot to Restore-AzVmSnapshot. This will find the snapshot created earlier and send it to be restored to the same VM it was taken on.
You'll also see that it'll prompt you to restore the snapshot because it shuts down the VM and relinks the snapshot VHD. You can also use the RemoveOriginalDisk parameter to remove the VHD previously attached to the VM.
PS> Get-AzVmSnapshot -ResourceGroupName 'XXXXXXX' -VmName XXXXX | Restore-AzVmSnapshot
Are you sure you want to perform this action?
Performing the operation "Restore" on target "Snapshot".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): a
A lot of PowerShell goes on inside these functions to make it happen, but by using the functions in the AzureVmSnapshots module, you don't have to worry about all of that.
At this point, you should now have a VHD attached to your original VM in the same state as when the snapshot was taken!