how to automate VM snapshots in Parallels Server

If you’re using Parallels Server for Mac, you might have discovered that the ‘Smart Guard’ feature (which automatically saves snapshots of your VM’s) causes all kinds of problems. Parallels has actually recommended that people turn this feature off until they fix it! Here’s an alternative method for automatic VM snapshots. You can call this script daily with iCal or however you like.

set theVms to {"fog", "kurzweil", "pstest", "webprint", "whd"} -- or whatever your VMS are named

tell application "Finder"
	
	repeat with vm in theVms
		--make new timestamped snapshots for each VM
		set myDate to (do shell script "date '+%m-%d-%Y|%H.%M.%S'") as text
		set createShell to "prlctl snapshot " & vm & " -n " & (quoted form of myDate)
		do shell script createShell
		
		set theDir to alias ("host:Users:Shared:Parallels:" & vm & ".pvm:Snapshots:") -- or wherever your VM files live
		-- delete snapshots more than a week old
		repeat with d in (theDir)
			if name extension of d is "mem" then
				if creation date of d < ((current date) - (7 * days)) then -- find files more than a week old
					set deleteShell to "prlctl snapshot-delete " & vm & " -i " & (characters 1 thru -5 of (name of d as text) as text)
					do shell script deleteShell
				end if
			end if
		end repeat
	end repeat
end tell