Reputation: 11
I have a batch file that starts a VirtualBox VM when windows starts and I'd like to have one that automatically saves the state of the VM when someone reboots or shuts down the machine.
I can't use GPO because everything it does happens after Windows has killed everything else.
So is there a way to modify how Windows handles this or maybe intercept the shutdown/reboot signal somehow?
Upvotes: 0
Views: 4074
Reputation: 11
I found this solution a while ago. It uses python so it is not the most lightweight solution but it worked for me.
- Download vbox-shutdown.py somewhere there are write permissions (it will create a logfile unless you specify it a different path).
- Python 2.7 with same architecture as VirtualBox - if you have 64bit system most probably you'll need a 64-bit Python (I've used the COM api - it doesn't allow cross arch interop for now). This could change by using the command line API if anyone really wanted this. Anyone can fix it - it's just some Python code.
- Install pywin32 - same architecture/reason as above.
- Install the vboxapi package. Running python vboxapisetup.py install in c:\Program Files\Oracle\VirtualBox\sdk\install\ did the trick for me. I wonder why they have this useless pypi package with no distributions ...
- Now just run shell:startup and put the file in there.
All the credits go to ionelmc http://blog.ionelmc.ro/2014/01/04/virtualbox-vm-auto-shutdown/
Upvotes: 1
Reputation: 418
5moufl you stated "I can't use GPO because everything it does happens after Windows has killed everything else". Can you elaborate on the local group policy change you tried? Were there any errors? What didn't work exactly for you? When the VM came up on a subsequent restart what happened?
There are details on defining a shutdown action here as a group policy: http://en.kioskea.net/faq/3358-execute-a-script-a-startup-and-shutdown :
Upvotes: 0
Reputation: 2071
Ok, best I could come up with
The following python script will wait for a shutdown, abort it, call your script, then restart the shutdown. Add it to start at startup.
EDIT requires pywin32 python extension availible Here
1 Problem is it won't abort a shutdown from the start menu or power button. Only software shutdowns. You can disable them like this and this. If you still want the ability to manually shutdown your pc, add a batch file NAMED ANYTHING BUT "SHUTDOWN" with the following contents
shutdown -f -s
python script
import win32security
import win32api
import sys
import time
from ntsecuritycon import *
import os
import subprocess
Pre_ShutdownScript = "your Script"
Shutdown = True
# Get the process token
flags = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY
htoken = win32security.OpenProcessToken(win32api.GetCurrentProcess(), flags)
# Get the ID for the system shutdown privilege.
idd = win32security.LookupPrivilegeValue(None, SE_SHUTDOWN_NAME)
while Shutdown:
try:
win32security.AdjustTokenPrivileges(htoken, 0, [(idd, SE_PRIVILEGE_ENABLED)])
win32api.AbortSystemShutdown(None)
Shutdown = False
except:
win32security.AdjustTokenPrivileges(htoken, 0, [(idd, 0)])
time.sleep(1)
try:
subprocess.call([Pre_ShutdownScript])
except:
pass
os.system("shutdown -r -t 1")
Upvotes: 0