How to save the state of a VirtualBox guest OS VM on a Windows host logout?


I am running an Ubuntu VM guest OS in headless mode on a Windows 7 host. I wrote a batch script which uses VBoxManage to start or save the state of the VM based on the passed in argument. I can start the VM just fine by putting a shortcut to the script in my startup folder, but I’m having trouble getting the VM to suspend on logout.

I tried a few different methods:

  1. Creating a User logoff scripts entry in the Local Group Policy Editor (gpedit.msc) runs my script, but the VBoxManage process reports that the VM isn’t running. I assume the logout process is killing the VM before my script can get to it.
  2. Creating a task through Task Scheduler that triggers on event 7002, which I'm told is the logoff event, runs the script, but VBoxManage reports the following:

VBoxManage.exe: error: Failed to create the VirtualBox object!

VBoxManage.exe: error: Code E_ACCESSDENIED (0x80070005) - General access denied error (extended info not available)

VBoxManage.exe: error: Most likely, the VirtualBox COM server is not running or failed to start.

Is there some better way to automatically suspend the state of a VM when logging off of Windows?

The contents of the batch script:

@echo off


rem list of VMs to start/stop
set vm_list=Ubuntu

set action=%1
if not "%action%" == "start" if not "%action%" == "stop" exit /B

set command="C:\Program Files\Oracle\VirtualBox\VboxManage.exe"
set logfile="C:\bin\vm_log.txt"

if exist %logfile% echo.>> %logfile%

echo %date% %time%>> %logfile%

for %%A in (%vm_list%) do (
    if "%action%" == "start" (
        echo Starting %%A>> %logfile% 2>&1
        %command% startvm %%A --type headless >> %logfile% 2>&1
    ) else if "%action%" == "stop" (
        echo Suspending %%A>> %logfile% 2>&1
        %command% controlvm %%A savestate >> %logfile% 2>&1


I have the output of the commands redirected to vm_log.txt which is how I am able to see the messages that were reported.

asked on Super User Sep 8, 2015 by Pak • edited Sep 9, 2015 by Pak

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0