0

I am testing a simple build process in Jenkins; The Build section has Execute Windows batch command

echo %ERRORLEVEL%
cscript C:\Users\user\Documents\test.vbs
echo %ERRORLEVEL%

My test.vbs does nothing except set the ERRORLEVEL environment variable; I also tried using wscript.quit to see if that had an effect on ERRORLEVEL

dim wShell
dim wSysEnv

set wShell = Wscript.CreateObject("WScript.Shell")
set wSysEnv = wShell.Environment("SYSTEM")

WScript.Echo "ERRORLEVEL=" & wSysEnv( "ERRORLEVEL" )
wSysEnv( "ERRORLEVEL" ) = 0
WScript.Echo "ERRORLEVEL=" & wSysEnv( "ERRORLEVEL" )
wSysEnv( "ERRORLEVEL" ) = 2
WScript.Echo "ERRORLEVEL=" & wSysEnv( "ERRORLEVEL" )
WScript.Quit 3

There is no post build action The console output is as follows:

Started by user anonymous
Building in workspace C:\Program Files (x86)\Jenkins\jobs\TADS Host-Agent client\workspace
[workspace] $ cmd /c call C:\Users\user\AppData\Local\Temp\hudson4571524647235360597.bat

C:\Program Files (x86)\Jenkins\jobs\TADS Host-Agent client\workspace>echo 1 
1

C:\Program Files (x86)\Jenkins\jobs\TADS Host-Agent client\workspace>cscript C:\Users\user\Documents\test.vbs 
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

ERRORLEVEL=0
ERRORLEVEL=0
ERRORLEVEL=2

C:\Program Files (x86)\Jenkins\jobs\TADS Host-Agent client\workspace>echo 1 
1

C:\Program Files (x86)\Jenkins\jobs\TADS Host-Agent client\workspace>exit 1 
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE

As shown in the output above, the ERRORLEVEL before I start the script is 1. The script successfully sets ERRORLEVEL to 2 and exits with a code of 3. However when I check the ERRORLEVEL at the end of the script, it is back to 1 as it started.

I found some pages that said environment variables are only set for the current process and any child process; that is they are session environment variables.

In which case how can ERRORLEVEL be set for Jenkins to report a test as success or failure? It doesn't appear to check the variable during runtime, only at the start of the execution of the script. This is not useful as the script may change the value of the variable.

A G
  • 665
  • 1
  • 11
  • 29
  • answer on SO http://stackoverflow.com/questions/3737725/how-to-set-environment-variables-in-vbs-that-can-be-read-in-calling-batch-script and still you can have a look at http://www.robvanderwoude.com/vbstech_data_environment.php – Papasmile Jul 17 '13 at 14:57

1 Answers1

1

%ERRORLEVEL% is an automatic variable, and you shouldn't tamper with it directly, even more since write-access to the SYSTEM environment requires admin privileges.

%ERRORLEVEL% should be automatically populated by the integer value passed to the Quit method. A VBScript with a line WScript.Quit 3 called by a batch script like this:

@echo off
echo %ERRORLEVEL%
cscript //NoLogo C:\path\to\your.vbs
echo %ERRORLEVEL%

should produce the following output:

C:\>C:\path\to\your.cmd
0
3
Ansgar Wiechers
  • 175,025
  • 22
  • 204
  • 278
  • How can I test why this isn't happening? The batch script is exactly as you have it and my vbs ends with `WScript.Quit 3`, yet it still shows `ERRORLEVEL` is 1 – A G Jul 18 '13 at 10:45
  • You must have manually set `%ERRORLEVEL%` elsewhere. Like I said: don't do that. `%ERRORLEVEL%` is an automatic variable. **DO. NOT. TAMPER. WITH. IT.** – Ansgar Wiechers Jul 18 '13 at 11:15
  • hhmm, not sure what was happening, opened up a new command window and it works as expected. I must have set it yesterday while experimenting. Thanks – A G Jul 18 '13 at 16:00