One of my regular tasks is snapshotting my demo environment – it’s crucial that all the VMs are simultaneously snapshotted to avoid configuration drift. I still try not to need to restore those snapshots, although I often create a snapshot when I import my VMs to saving having to repeat that step often. Here’s my VM snapshotting script:
$snapshotname = "FreshInstall" $servername = "server" $VMNamePatterns="Project-" cls #Get-VM | where vmname -like *$VMNamePatterns* | Checkpoint-VM -SnapshotName $snapshotname -AsJob Get-VM | where vmname -like *$VMNamePatterns* | Restore-VMSnapshot -Name $snapshotname -AsJob #Get-VM | where vmname -like *$VMNamePatterns* | Remove-VMSnapshot -Name $snapshotname -asjob echo "" echo "All done!" pause
Using this script is fairly simple – I could have created a menu etc. but it’s just not worth it to me. A couple of important notes:
- I use a “pattern” or project name for each group of VMs I import. I have a rename script which I’ll post later. This pattern is always before my VM “names” but it part of the VMname.
- I take a list of the VMs, filter it by the pattern, then either checkpoint, restore or remove snapshots based on the line note commented out.
This script can result in multiple identical snapshot names, which can be confusing for you and for Hyper-V! When you have two snapshots with the same name and try to restore you’ll find the most recent checkpoint is restored. When you use the remove line with two snapshots with the same name, you’ll find that both are removed….the moral of the story is change the snapshot name.