Skip to content

Instantly share code, notes, and snippets.

@mkorthof
Created August 24, 2017 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkorthof/deef752e03305a16e46f0e179b036820 to your computer and use it in GitHub Desktop.
Save mkorthof/deef752e03305a16e46f0e179b036820 to your computer and use it in GitHub Desktop.
VirtualBox savestate all running VM's (Windows)
@echo off
:: https://superuser.com/questions/959567/virtualbox-windows-graceful-shutdown-of-guests-on-host-shutdown
SET "LOG=0"
SET "LOGFILE=%~dp0%vboxsave.log"
SET VBoxManage="%ProgramFiles%\Oracle\VirtualBox\VBoxManage.exe"
IF "%~1"=="LOG" ( SET "LOG=1" )
IF EXIST "%LOGFILE%.tmp" ( del %LOGFILE%.tmp )
IF %LOG% EQU 1 ( CALL :msg logging is enabled )
CALL :msg starting %VBoxManage% list runningvms...
FOR /F tokens^=2^,4^ delims^=^" %%p IN ('%VBoxManage% list runningvms') DO (
CALL :msg running controlvm %%p savestate:
%VBoxManage% controlvm %%p savestate >%LOGFILE%.tmp 2>&1
)
IF EXIST "%LOGFILE%.tmp" (
type %LOGFILE%.tmp
IF %LOG% EQU 1 ( type %LOGFILE%.tmp >> %LOGFILE% )
del %LOGFILE%.tmp
)
CALL :msg done
GOTO :EOF
:msg
FOR /f "delims=" %%# IN ('powershell.exe Get-Date -Format G') do @set cdate=%%#
echo %cdate% %*
IF %LOG% EQU 1 ( echo %cdate% %* >> %LOGFILE% )
GOTO :EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment