VMware Cloud Community
eggwigg
Contributor
Contributor

How to schedule revert to snapshot? (cron job?, virtualcentre?)

Hi,

I need to schedule a job to revert toa snapshot on a guest OS every night following a day's training.

The scheduled tasks in Virtual Center only has take a snapshot as an option; I've found an option on the host console using "vmware-cmd cfg revertsnapshot" but don't know how to create a script / cron job on this.

Can anyone suggets how to either create a custon scheduled task in Virtualcentre or how to create a cron job on the host to achieve this?

For extra credit :smileyalert: it would be helpful to be able to set up an email notification that the has run successfully.

Thanks for looking:)

regards

Guy

0 Kudos
3 Replies
wardb0071
Enthusiast
Enthusiast

Not sure if this is what you want, but one option you may be able to use is a Nonpersistent Disk - The disk appears to operate normally while the virtual machine is powered on, but when it is powered off, the disk contents revert to the original contents.

So at the end of your training just schedlue your PC to Power off (windows command shudown.exe -s) then when you power on the next day its a clean machine.

Cheers,

Brian

Troy_Clavell
Immortal
Immortal

eggwigg
Contributor
Contributor

Hi,

Thanks for both your helpful answers - I'm almost there.

The powershell tools did the job - the non persisitent disks are a good idea, but if a machine needed tobe rebooted or something happened I can't risk losing a course half way thorugh.

one bizarre issue though; run from a command line, the code reverts to snapshot perfectly and logs task / even as "revert to snapshot". When called from a script (which is what I need to schedule), it logs the event / task as "Reconfigured virtual machine"and does not revert to snapshot.

Any ideas? could this be a bug?

SCRIPT:

add-pssnapin VMware.VimAutomation.Core

$file = "C:\snap.log"

$file2 = "C:\snap_VIevents.log"

$server = connect-viserver -server 10.20.255.5 | out-file -FilePath $file

$workingVM = Get-VM -Name D1TRNMFSFRS01

$workingSnapshot = Get-Snapshot -Name "TEST" -VM $workingVM | out-file -FilePath $file -append

$DTG = Get-Date

Set-VM -Snapshot $workingSnapshot $workingVM -Confirm:$false | out-file -FilePath $file -append

get-vievent -Entity $workingVM -start (get-date -displayhint date -uformat %d/%m/%Y) | out-file -FilePath $file2

$SmtpClient = new-object system.net.mail.smtpClient

$SmtpServer = "xxxxxxxxxxx"

$From =

$To = "xxxxxxxx"

$Cc = "xxxxxxxxxx"

$Title = "Daily DE TRN Snapshot Report for " + $DTG

$Body = "The script has run for today"

$msg = new-object System.Net.Mail.MailMessage $From, $To, $Title, $Body

#$msg.TO.add($Cc)

$attachment1 = new-object System.Net.Mail.Attachment $file

$attachment2 = new-object System.Net.Mail.Attachment $file2

$msg.Attachments.Add($attachment1)

$msg.Attachments.Add($attachment2)

$client = new-object System.Net.Mail.SmtpClient $SmtpServer

$client.Send($msg)

BATCH to call script:

powershell -command "& 'h:\scheduledscripts\DE_TRN_Snap.ps1' "

thanks in advance - this is a weird one!

0 Kudos