Freigeben über


WScript.Quit( Value ) does not always match the error code environment variable %errorlevel% in the command processor ( batch file processing ) environment.

In the windows scripting host environment, the WScript.Quit method can be used to return an exit code to a calling process. In 99.9% of the cases, the value set with the WSCript.Quit method will be returned as expected. If you are running your script code in a command window, the value usually comes back in the errorlevel environment variable. The exact circumstances when the WScript.Quit method call will not match the exit code value outside the scripting host have not been clearly defined, but appears to be related to running scripts from within the batch processor inside the cmd.exe hosting environment.

In my empirical testing, I have not been able to reproduce this particular issue. However, I have had customers reporting that this issue occurs randomly and is not predictable. This behavior has plagued the scripting hosts ( both, wscript and cscript engines) since their creation. Customers have made change requests for the scripting engines to insure that 100% of the time the value returned by the WScript.Quit method will match the errorlevel environment variable in the command shell. While fixes have been made to the hosting environments, the 100% guaranteed match of the value returned from WScript.Quit to the errorlevel environment variable remains elusive.

On the other hand, at present, no one has reported this same issue when using the cscript engine in conjunction with the Powershell default command environment. The issue appears to be limited to the command processing environment ( cmd.exe ) and not necessarily a problem with the scripting engine itself. Rather, more of a process communication issue within the hosting environment (ie the cmd.exe/batch processing environment).

The conclusion being that the programmer should be aware that under some circumstances, the cscript/wscript Visual Basic Script engines return values from the WScript.Quit method may not match the value that the command processor environment (cmd.exe ) places into the errorlevel environment variable.

To work around the issue one can have their script write the exit value in the registry, a custom environment variable or perhaps one of the system/application event logs as an event entry then read the value using another command in the hosting environment.

Comments