Shutdown VirtualBox VMs when PC shutdown

Soldato
Joined
28 May 2010
Posts
4,800
Location
London, UK
Hi
I have a PC with 2 x Oracle VirtualBox VMs running for testing purposes(for now).
I have setup a .bat file with the following command;

========================================
cd "C:\Program Files\Oracle\VirtualBox\"
VBoxManage.exe startvm "VM1"
VBoxManage.exe startvm "VM2"

========================================

Then I have setup a task in windows task scheduler to auto execute the above .bat file upon system startup.

I know the command to shut down the VMs,

==========================================
cd "C:\Program Files\Oracle\VirtualBox\"
VBoxManage.exe controlvm "VM1" acpipowerbutton
VBoxManage.exe controlvm "VM2" acpipowerbutton

===========================================

But what I can not figure out is how to create a task to execute on system shutdown(and restarts)!

TLDR:
Want to automatically start and shutdown VMs on PCs start and shutdown(and restart) events.


Any ideas?
 
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm VMNAME1 savestate
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm VMNAME2 savestate
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm "VMNAME 3" savestate
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm VMNAME4 savestate
shutdown /p /f


That saves them instead of shutting them down, this starts them..

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm VMNAME1


What I do is save the top section as 'shutdown script.bat' and use that to shutdown my PC instead of using the built in shutdown icon. I'm sure there is a way of setting things to run at logoff/shutdown, but they don't always wait for all tasks to finish. Doing it with this bat file means it HAS to finish before starting shutdown.
 
Last edited:
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm VMNAME1 savestate
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm VMNAME2 savestate
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm "VMNAME 3" savestate
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm VMNAME4 savestate
shutdown /p /f


That saves them instead of shutting them down, this starts them..

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm VMNAME1

As said in the op, I have managed to put the startvm command to a batch file and set it to run on host PC's startup event from task scheduler.

How do I get the savestate to happen when the Host PC is shutting down or restarting?
 
re-read my edited post.

I just double click the bat file on my desktop instead of using 'shutdown'

it goes through the motions, then shuts down. Would be simple to create one for restarting.
 
Ahhh ok...I didn't see the last part of your post when I replied! :)

I may have to use your solution until I find a better one - Thank you!
I was thinking of leaving the Host PC to fully automate everything and leave it as a headless host VM box!
But when the windows updates run and requires a restart it created a problem for the VMs - that's the reason for trying to automate the shutdowns for VMs on Host shutdown/restart event.
 
I see

What I do, is disable the auto-restart for Windows updates, it's a pain when it does it at a vital time. Then I'll just use my bat file to restart when I see fit.
 
Back
Top Bottom