-2

I'd like to write a script that restore a Virual Machine in Hyper-V from a snapshot once every 20 days.

I know that the automation can be achieved by using Windows Task Scheduler. The main problem is that I cannot really write that script by myself. I'd appreciate some guidance in the topic. I run Microsoft Server 2012 R2.

2

2 Answers 2

0

You could use the VMSnapshot applets. Example for retrieve the snappshots of a VM on HYPERV server :

Get-VMSnapshot -computername shyperv3 Server1

VMName      Name                                                    SnapshotType CreationTime        ParentSnapshotName                   
------      ----                                                    ------------ ------------        ------------------                   
Server1 Server1 - (16/08/2017 - 14:55:33)                   Standard     01/01/1601 01:00:00                                      
Server1 Server1 - (21/08/2017 - 12:11:24)                   Standard     21/08/2017 12:11:29 Server1 - (16/08/2017 - 14:55:33)
Server1 Server1 - (21/08/2017 - 16:06:58) OK                Standard     21/08/2017 16:07:03 Server1 - (21/08/2017 - 12:11:24)

Moreover, for restoring a VM, you can use Restore-VMSnapshot Example : https://technet.microsoft.com/en-us/itpro/powershell/windows/hyper-v/restore-vmsnapshot

0

Thanks for a reply. Turns out the solution was pretty simple. Note that it only works when you want to restore it from latest snapshot. In my case I only need one snapshot. I went to PowerShell ISE and wrote this script:

PS C:\> Get-VM | Foreach-Object { $_ | Get-VMSnapshot | Sort CreationTime | Select -Last 1 | Restore-VMSnapshot -Confirm:$false }

Like I said, it takes the latest snapshot and loads it without a need for confirmation. Then I proceed to Task Scheduler inside Server Manager and Create a Basic Task. Set the date and load the script file that you created in PowerShell ISE. Works like a charm. I hope someone finds it useful.

Not the answer you're looking for? Browse other questions tagged .