Search code examples
windows-7automationvirtualboxshutdownreboot

How do I execute commands before shutdown in Windows 7 (and before it kills all running programs?)


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?


Solution

  • 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/