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:
controlvm
command to perform ACPI shutdown of target VM. FOR
to iterate through a list of Running VMs to see if target VM is still running. 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:
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.