Build and Restore Snapshots in Hyper-V with Windows PowerShell
Summary: Microsoft PowerShell MVP, Sean Kearney, talks about using Windows PowerShell to build and restore snapshots in Hyper-V. Microsoft Scripting Guy, Ed Wilson, is here. If you are a seasoned Hey, Scripting Guy! Blog reader, you know that the most frequent guest blogger is Sean Kearney. If you are new to the blog, I welcome you, and I encourage you to catch up with Sean’s previous blogs. Sean is a Windows PowerShell MVP and an Honorary Scripting Guy. Sean has been selected to present sessions called Integrating with Microsoft System Center 2012 and Windows PowerShell at TechEd NA and TechEd Europe this year. In his free time, Sean has written several blog posts about Hyper-V and some other cool stuff, and for the next few weeks, Sean will be the designated guest blogger on Fridays. Take it away Sean… With Microsoft TechEd Europe and a few other presentations on the horizon, I was thinking about how nice it would be have my environments self manage for the demos. Typically with most demos in a virtual environment, you make a snapshot of everything in the Gold setup. Then when you enter into your presentations, you can immediately roll back to the original configuration to prepare for your next presentation. Or perhaps you’re firing up a test environment, and you need to be able to take a snapshot and restore sets of machines to troubleshoot a package issue. With Windows Server 2012 and the new Hyper-V module, this is as natural as breathing. Let’s say we have a list of virtual machines in our environment: a domain controller, a SQL Server, and perhaps our web application. We’ll name them DC1, SQL1, WEB1. Although these are the NetBios names, for convenience I have given them the same names in Hyper-V.
$VMNames=”DC1”,”SQL1”,”WEB1” To create a checkpoint on the virtual machines, we can simply use the Checkpoint-VM cmdlet and run it like this:
CHECKPOINT-VM –Name DC1 –Snapshotname ‘Base’ This will not only create a snapshot, but it will have a unique name that we can refer to. To run this against a list of machines, we can pipe the list by using Get-VM or supply a list like in our variable:
$VMNames | GET-VM | CHECKPOINT-VM –SnapshotName ‘Base’ This will create a snapshot named “Base” on the virtual machines named DC1, SQL1, and WEB1. If we’d like to access all of these snapshots, we can run this command:
$VMNames | GET-VM | GET-VMSnapshot –Name ‘Base’ We can now take this list and restore them:
$VMNames | GET-VM | GET-VMSnapshot –Name ‘Base’ | RESTORE-VMSnapshot –confirm:$False
One line and now I can restore any demo by using Windows Powershell. With very minor changes, I can create multiple snapshots with unique names by changing the name of snapshot.
Feel the Power within you.
The Energized Tech Great job, Sean. Join us tomorrow for more cool Windows PowerShell stuff. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy