I wrote this vbscript to Shutdown a known virtual server. It uses the GuestOS function to shutdown the operating system cleanly. (VM Additions must be installed for this to work).
It uses the VMTask output to ensure it waits until the server is shutdown before carrying out the next line of code. The -1 value indicates an unlimited time (use with extreme caution!). It then discards the Undo disks - I plan to run this on my Virtual network every night to clear any configurations and testings any co-workers have made through the day - so each day starts with a clean, trusted image.
Code :
set virtualserver = createobject("virtualserver.application")
set curvm = virtualserver.findvirtualmachine("My Server")
set CurOSint = curvm.GuestOS
set vmtask1 = CurOSint.shutdown
vmtask1.WaitForCompletion(-1)
curvm.DiscardUndoDisks
Although I need to discard undo disks on my virtual servers daily as I also run Active Directory, this would eventually upset replication and AD timestamps etc. Several hours after discarding undo disks in the evening I wanted to schedule a merge of the undo disks daily - so timestamps, replication and any other time / date related info can be added to the master disk. The script will save the servers state, wait for the VMTask to output the job has completed, and merge the Undo disk.
Code :
set virtualserver = createobject("virtualserver.application")
set curvm = virtualserver.findvirtualmachine("My Server")
set vmtask1 = curvm.save
vmtask1.WaitForCompletion(-1)
set vmtask2= curvm.mergeundodisks
vmtask2.WaitForCompletion(-1)