How to wait until VirtualBox has finished loading to execute next command in Windows Batch? How to wait until VirtualBox has finished loading to execute next command in Windows Batch? shell shell

How to wait until VirtualBox has finished loading to execute next command in Windows Batch?


Wow! Thanks paulm for inspiration.

Found one great opportunity!

I was browsing build-it guest OS properties with command :

 VBoxControl guestproperty enumerate -patterns *

and I've found a nice property "/VirtualBox/GuestInfo/OS/LoggedInUsers"

During guest OS boot this property changes from "No value set!" through value "0" to "1" (if user logs in automatically).Works fine on both Windows and linux.

>VBoxManage guestproperty get "WIN7_32" "/VirtualBox/GuestInfo/OS/LoggedInUsers"No value set!>VBoxManage guestproperty get "WIN7_32" "/VirtualBox/GuestInfo/OS/LoggedInUsers"Value: 0>VBoxManage guestproperty get "WIN7_32" "/VirtualBox/GuestInfo/OS/LoggedInUsers"Value: 1

So you don't need to add anything to Start-up on your guest OS! Sweet!

Also you can even use wait :

VBoxManage guestproperty wait "WIN7_32" "/VirtualBox/GuestInfo/OS/LoggedInUsers"

but keep in mind, that this command will return 0 once guest OS reaches Welcome screen


The "best" way to do this is by using VBoxManage on the host machine combined with VBoxControl on the guest machine using properties.

The way this works is that the host machine will wait for a property to be set, and the guest machine will set this property when ever you consider the machine to have "loaded". For me I simply set the property from a logon script.

So on my Linux host I say:

VBoxManage guestproperty wait My_Virtual_Machine_Name Wait_For_Logon_Event

This will wait/block forever, you can add a timeout if you wish.

Then on the Windows Guest in a logon script or machine start script I execute:

VBoxControl guestproperty set Wait_For_Logon_Event Event_Now_Set

This then causes the execution to continue on the host side:

Name: Wait_For_Logon_Event, value: Event_Now_Set, flags:


As you have observed, the VirtualBox/VBoxManage process will return and exit once the VM has actually turned on. There isn't a good way to hook into the guest at start, though one thing you can do is instruct VirtualBox to run a command and check for the exit code. I would insert this code in your batch file script right after starting the guest VM but before you want to start sh.exe:

:CheckVBoxVBoxManage guestcontrol Win8 exec --image c:\Windows\System32\ipconfig.exe --wait-exit --username Goyuix --password VirtualBoxRoxIF NOT ERRORLEVEL 0 (  REM Do something to sleep here, pinging localhost or timeout  GOTO CheckVBox)REM VM Ready, go ahead and fire up other apps that depend on it

Note: you will need to have the path to the VBoxManage executable in your PATH environment variable. On Windows, it is typically: C:\Program Files\Oracle\VirtualBox

Granted, you would probably want something more robust that just running ipconfig to detect if the VM is really ready. I am not sure what services etc. you would depend on having available, but this might be good enough to get you over the hump and can certainly be adapted to check for service status etc.