Creating (and deleting) checkpoints for multi-VM sets in Hyper-V

When writing custom PowerShell DSC, I am typically working with complex configurations that involve not only multiple software installs, but database creation and domain join, resulting in changes on multiple VMs. In order to rollback operations on a front-end server, back-end SQL and an AD domain controller, I need snapshots of all VMs at the same point in time. As a quick reminder to myself, I wanted to take a moment to post a couple of sample PowerShell snippets for quickly creating or deleting snapshots on all the VMs in a scenario at once.

I wanted to take a moment to post a couple of sample PowerShell snippets for quickly creating or deleting snapshots on all the VMs in a scenario at once. It’s a time-saver when I want to rollback an entire scenario or take accurate and easily referenceable, incremental checkpoints of my scenarios as I progress. VERY simple, VERY effective, HUGE time-saver.

I prefer production checkpoints, which are the default in Hyper-V on Windows 10. If you want to be sure, look in your VM properties as shown in the image below.

Hyper-V Snapshot

I run scenarios Hyper-V on a Windows 1o laptop (a mobile server really), so I keep my VMs straight by giving them a common naming prefix. For a SCOM test, I may use a prefix in Hyper-V like “SCOM-“.

Creating checkpoints in bulk

Here is a simple PowerShell snippets for creating checkpoints on multiple VMs at once. You will notice it assumes your VMs have a common prefix on their name. A date is appended to the name you provide for the snapshot so you know when you took it at-a-glance.

Function CreateCheckPoint ($VMName, $SnapShotName) {

$VMs = Get-VM | WHERE {$_.Name -like "$VMName"}

$Date = Get-Date -Format g

foreach ($VM in $VMs){

Checkpoint-VM -Name $VM.Name -SnapshotName "$SnapshotName - $Date"

}
}

Call the function above like so, replacing parameter values appropriate to your environment.

CreateCheckPoint -VMName "2016*" -SnapShotName "Pre-2016 upgrade"

Deleting checkpoints in bulk

Here is a simple PowerShell snippets for deleting checkpoints on multiple VMs at once, based on VM naming prefix and description. I put a -confirm parameter on Remove-Snapshot so I am prompted to review and accept deletion. You can respond to the prompt to “accept all”, so it’s a nice failsafe to ensure you don’t delete the wrong snapshot.

Function RemoveCheckPoint ($VMName, $SnapShotName) {

Get-VMSnapshot -VMName $VMName | WHERE {$_.Name -like "$SnapShotName"} | Remove-VMSnapshot -Confirm
}

RemoveCheckPoint -VMName “2016*” -SnapShotName “Pre-2016 upgrade*”

Call the function above like so, replacing parameter values appropriate to your environment.

RemoveCheckPoint -VMName "2016*" -SnapShotName "Pre-2016 upgrade*"
Print Friendly
Posted in Blog

Leave a Reply

%d bloggers like this: