top of page

Automate Hyper-V Host Virtual Machines (VMs) Snapshot using PowerShell


While the Hyper-V management console is cozy and can certainly get the job done, I think there are a handful of PowerShell cmdlets that can make your life easier.

In our environment, we have a bunch of Windows Server 2012 R2 core Edition as hyper-v host with 5o odd VMs running on them. If you are in similar sort of situation and wondering how to quickly restore them if there is a problem, then think about a strategy which meet your needs. It's important to have onsite or offsite backup for disaster recovery, however this article is primarily about Hyper-V Host VMs snapshot to roll back quickly to earlier point. The goal here is to create a snapshot and maintain a limited number of snapshots for a month. This way you can reduce storage issues. However it entirely up to you to decide what suits your needs.

All references are to Hyper-V cmdlets that run on PowerShell 3.0.

So Let us get Started.

Since our is core OS edition without GUI, first we need to create a folder under the root of C-Drive to hold the PowerShell scripts. So if you manage your hyper-v from server manager, right click on Hyper-V host server and select Windows Powershell.

1. Create a folder to store your scripts:

New-Item C:\Scripts –ItemType Directory

2. Create these two PowerShell scripts.

The below is meant to do a weekly snapshot and delete any snapshot older than 31days. Please modify the bit relevant to you, then copy and paste the text from comment which start from # sign into corresponding scripts. These are pretty generic scripts, once you have done that on first Hypervisor server. Simply copy and paste scripts folders on other Hypervisor hosts directly, unless you have different schedule for others.

VMCheckPointWeeklyJob.PS1

#This script was created to run weekly snapshots of VMs

$vm = Get-VM | checkpoint-vm -SnapshotName "Weekly Snapshot $((Get-Date).toshortdatestring())" –AsJob

VMRemoveCheckpoints.PS1

#Script to Remove VM Snapshots

Get-VMSnapshot -VMName * | Where-Object {$_.CreationTime -lt (Get-Date).AddDays(-31) } | Remove-VMSnapshot

If you notice in the first script, we create a variable '$VM' to store the result from Get-VM cmdlets, then it pipe to run the checkpoint.

In Second script, we used a wildcard '*' to query all VMs to report the snapshot history across all VMs.

3. Set the Task Scheduler to automate this process

Create two tasks, as per your schedule.

Result:

If you need help with your Hyper-V, please don't hesitate to get in contact with us, or join us at the UK Azure User Group or UK Cloud Infrastructure User Group meetups.

 

Vinnie is an Azure Principal Consultant at Elastabytes. He is originally from India and now living in London. His interests include Film, Fitness, and Cloud Solutions.

You can read Vinnie's personal IT blog here and you can follow him on Twitter @VinnieGrack.

 

Elastabytes are a cloud managed services company specialising in SQL Server. We’re a Microsoft Gold Partner and our team is made of Microsoft MVPs. Whether you’re already in Azure and need a DBA-as-a-Service, or you’re about to go to Azure and need a hand getting there, Elastabytes can help.

Contact us at info@elastabytes.com to set up a chat with one of our architects.

You can follow us on Twitter @elastabytes.

 

Featured Posts
Recent Posts
Archive
Search By Tags
No tags yet.
Follow Us
  • LinkedIn Social Icon
  • Twitter Basic Square
  • Facebook Basic Square
  • Google+ Basic Square
bottom of page