How To Use PowerShell for Bulk VM Checkpointing
This process is perfect for those looking to create restore points before updates are installed.
Although Hyper-V checkpoints are not a substitute for backups, they do have their place. For example, some people like to create virtual machine checkpoints prior to installing updates. That way, if something were to go wrong with an update, then the virtual machine can easily be reverted back to its previous state.
This technique works well if you only need to update a few virtual machines. But what if you have a large number of virtual machines that need to be updated, and you want to create checkpoints on all of them? Well, in this type of situation, PowerShell can make quick work of an otherwise impractical task.
Before I show you how to use PowerShell to checkpoint large numbers of virtual machines, I need to point out that Microsoft does not currently recommend the use of checkpoints on production virtual machines, because checkpoints are not application aware. Microsoft is introducing a production checkpoint feature in Windows Server 2016 that will allow for safe checkpointing of virtualized application servers, because the checkpoint feature will leverage the Volume Shadow Copy services.
With that said, let's go ahead and look at how create checkpoints in bulk. The PowerShell cmdlet for creating a checkpoint is Checkpoint-VM. It would be really easy to use the Get-VM cmdlet to retrieve a list of the virtual machines that reside on a Hyper-V host, and then use the Checkpoint-VM cmdlet to create checkpoints for all of those virtual machines. However, we need to think about what is going to happen to the checkpoints in the future.
When you create a Hyper-V checkpoint, you are actually creating a differencing disk. The creation of a virtual machine checkpoint negatively impacts virtual machine read performance as a result of the heretical relationship between the parent virtual hard disk and the differencing disk. A single checkpoint does not usually make a huge difference in virtual machine performance, but the accumulation of checkpoints can have a very noticeable effect on performance. The more checkpoints exist for a virtual machine, the greater the impact on read performance. As such, it is important to remove checkpoints once they are no longer needed.
As previously noted, PowerShell makes it easy to checkpoint all of your virtual machines prior to installing updates. Assuming that the updates are successful however, there will come a point when you will probably want to remove those checkpoints. Just as manually creating large numbers of checkpoints would be a tedious process, manually removing those checkpoints would also be tedious.
As you would probably assume, PowerShell includes a cmdlet for removing checkpoints. However, you are going to need a way of differentiating one checkpoint from another. That way, you can tell PowerShell which checkpoints to remove. The trick to making this process work is to assign your checkpoints a name. Suppose for instance that you are creating checkpoints before installing an update. You might name all of your checkpoints BeforeUpdate. It's OK if all of your checkpoints use the same name, because the checkpoints are being applied to different virtual machines. If anything, using a common checkpoint name for each virtual machine is a good thing, because when the checkpoints are no longer needed you can tell PowerShell to delete all of the checkpoints that are named BeforeUpdate (or whatever name you decide to use).
So here is how the process works. We can checkpoint all of the virtual machines that reside on a Hyper-V server by using this command:
Get-VM | Checkpoint-VM –SnapshotName BeforeUpdate
As you can see, the SnapshotName switch is used to specify the checkpoint name. You can see what this looks like in Figure 1.
So what if we wanted to confirm the existence of the checkpoints that we just created? We could use the Hyper-V Manager to spot check the checkpoints, or we could use PowerShell. One way of doing this is to use this command:
Get-VM | Get-VMSnapshot
As you can see in Figure 2, this command lists every checkpoint that exists on the virtual machines, including those that existed prior to the creation of the BeforeUpdate checkpoint. If you wanted to filter the results to show only the BeforeUpdate checkpoint, you could do so by using this command:
Get-VM | Get-VMSnapshot –Name BeforeUpdate
OK, so let's assume that you have applied the Windows updates, and everything went smoothly so now you are ready to remove the checkpoints. As you saw in Figure 2, a couple of my VMs contained checkpoints that existed prior to the creation of the BeforeUpdate checkpoint, and I don't want to remove those checkpoints. I only want to get rid of the checkpoints named BeforeUpdate. To delete the BeforeUpdate checkpoints, enter the following command:
Get-VM | Get-VMSnapshot –Name BeforeUpdate | Remove-VMSnapshot
You can see an example of this command being used in Figure 4. The figure also uses the Get-VM | Get-VMSnapshot command to confirm that the BeforeUpdate snapshots have been removed, while the previously existing snapshots remain.
Brien Posey is a 19-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.