batch-fileerror-handlingerrorlevelinstallutil

Retrieving exit status of InstallUtil with ERRORLEVEL


UPDATE.

When writing this question I was assuming wrongly that InstallUtil stores its status in the %ERRORLEVEL% variable rather than the internal value ERRORLEVEL, due to its unexpected behavior. I rephrased the title since it could be misleading. See This answer and my comment below it for more details.

(As a side note, Microsoft doesn't document the exit status codes, nor does it specify if there is more than one error status.)


I'm writing installation and uninstallation scripts for a Windows Service. I use InstallUtil provided with the .NET Framework.

In a SO answer, I noticed this piece of code:

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
installutil.exe "C:\Services\myservice.exe"

if ERRORLEVEL 1 goto error

The ERRORLEVEL check always fails. Out of curiosity, I replaced the test above by this line:

IF NOT '%ERRORLEVEL%' == '0'

This time, the status is correctly caught. Why?

Before asking I read ERRORLEVEL is not %ERRORLEVEL%, but I still don't understant why InstallUtil behaves this way.


Solution

  • Since you've stated that installutil returns -1 if it fails, if errorlevel 1 goto error will never get picked up because if errorlevel 1 means "if %errorlevel% is 1 or higher," and -1 is less than 1.

    if not '%errorlevel%'=='0' works because -1 is not 0.