简体   繁体   中英

How to check via command line if a VirtualBox VM is not running on Windows

I am working on a Jenkins project to remotely startup and shutdown a specific VM with user-supplied parameters.

I have managed to find a way to determine that the specified VM is started up and running and ready for further instructions, but I have yet to find a way to determine that the specified VM has powered off.

The current code used in the build step is this:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

ECHO Shutting down VM %VM% on Host %computername%...
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm %VM% acpipowerbutton
ECHO %ERRORLEVEL%
IF ERRORLEVEL 1 EXIT /B 1

:LOOP
WAITFOR /T 10 VMtoShutDown 2>NUL
FOR /f %%a IN ('VBoxManage list runningvms ^| FINDSTR %VM%') DO SET VM_State=%%a
IF [!VM_State!] EQU [] (
ECHO %VM% is not running.
EXIT /b 0
)
GOTO LOOP

The logic is this:

  1. Send controlvm command to perform ACPI shutdown of target VM.
  2. Use FOR to iterate through a list of Running VMs to see if target VM is still running.
  3. If target VM is still running, don't do anything, and go back to the beginning of :LOOP and re-check until result changes.
  4. If target VM is no longer seen in the list of Running VMs, consider this build step a success.

The main problem is the FOR statement in the :LOOP clause. In the Startup portion of the build step, I can use VboxManage list runningvms to determine if the specified VM is listed, and thus process it as such and indicate that the Jenkins build to startup the specified VM is successful.

However, I noticed that once the target VM is taken off the list of running VMs by way of the earlier controlvm command, the entire DO part of the FOR statement simply doesn't do anything. It doesn't set the VM_State=%%a , for one, and it doesn't do anything else I had thrown into the FOR statement. This causes the :LOOP to keep looping infinitely as the only condition checker doesn't fire.

Question: Is there any way to determine if a VM of a particular name is powered off?

Notes:

  • The VirtualBox plugin for Jenkins is incompatible with the current VirtualBox version (5.1.30).
  • The Jenkins version is 2.122.
  • The code above is tested to behave the same when running as a standalone .bat file on the target machine and on my own machine.
  • All systems involved are on Windows 10 v1803.

This works for me on Windows 10, hope it helps!

@ECHO OFF
SETLOCAL EnableDelayedExpansion

ECHO Shutting down VM %VM% on Host %computername%...
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm %VM% acpipowerbutton
ECHO VirtualBox command return code: %ERRORLEVEL%
IF ERRORLEVEL 1 EXIT /B 1

:LOOP
REM Sleep for 1 second by using a ping hack :)
ping -n 2 127.0.0.1 > nul
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" showvminfo %VM% | findstr /c:"powered off" >NUL
IF %ERRORLEVEL% EQU 0 GOTO END
echo VM %VM% not powered off yet...
GOTO LOOP

:END
echo VM %VM% powered off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM