Question

I have a simple batch file to deploy a VM image (.ova file), then import it into VirtualBox and start it.

Here are the last lines of the script:

call "%vboxmanage_exe%"  import "%dest_path%\%latest_vm_filename%" --vsys 0 --vmname %vm_name%
IF %ERRORLEVEL% neq 0 (
  ECHO VM import failed!
  EXIT /b 1
)

call "%vboxmanage_exe%" startvm "%vm_name%" --type headless
IF %ERRORLEVEL% neq 0 (
  ECHO VM start failed!
  EXIT /b 1
)

The start command actually looks like this:

VBoxManage.exe startvm "D:\Vms\Latest.ova" --type headless

When there's already a VM with the name I am specifying in the command, there's an error and my script catches it (the echo and exit both execute).

However, if the import succeeds and it tries to start it, and fails, the error is not caught and it continues... The startvm command always returns an exit code equals to 0?

Here is the error I have when I try to start the VM (introduced to test the mechanics).

VBoxManage.exe: error: Nonexistent host networking interface, name 'Broadcom NetXtreme 57xx Gigabit Controller' (VERR_INTERNAL_ERROR)
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component Console, interface IConsole

How do I know if the start failed, to exit from my batch file with an appropriate error code. I guess I can check the VM status to see if it's running (did not look into that possibility yet), but I would prefer not to do that and get the actually error from the startvm command.

EDIT: The only thing I found that was remotly similar is this: batch: Exit code for "rd" is 0 on error as well

I tried the suggested solution, but it did not work for me.

Was it helpful?

Solution

Given the extra information, try this:

call "%vboxmanage_exe%" startvm "%vm_name%" --type headless 2>&1 |find "error:" >nul && exit /b 1

It assumes that the error messages that you provided are on the console and not in a log file, which would need a different approach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top