0

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.

Amessihel
  • 5,891
  • 3
  • 16
  • 40
  • 1
    What is the output if you `echo %errorlevel%` immediately after the `installutil` command? – SomethingDark Jun 30 '20 at 00:58
  • 1
    @SomethingDark `-1` if it fails, `0` it it succeeds. – Amessihel Jun 30 '20 at 01:04
  • `If ErrorLevel 1` means if the error code returned as `1` or greater, `If Not %ErrorLevel% == 0` means if the error is greater than, or less than, `0`. Clearly as `-1` is less that `0`, the latter command catches it appropriately, whereas the former fails. The moral being that you find out what your command is using for an error code, before asking to preform an action based upon it. – Compo Jun 30 '20 at 01:27
  • If I was having an issue with a command, my first port of call would be to find and read the help information on that particular command. It would not be to ask other people, before dong any investigation. Perhaps the lack of sleep can be blamed, because to me, it was immediately obvious, when I read your question, that the error status code was negative. The first thing I'd have done, to test my theory would have been to do what @SomethingDark asked you to do! – Compo Jun 30 '20 at 10:51
  • I'm not assuming anything, @Amessihel. If it isn't `0` and it isn't greater than `0`, then the logical port of call should have been to assume it is less than `0` or none existent! You shouldn't have needed to do any searches, just one command, to find out. – Compo Jun 30 '20 at 11:21

1 Answers1

2

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.

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • Yes, this looks obvous now. I was somehow persuaded `InstallUtil` touched the variable `%ErrorLevel%` rather than setting an exit status retrievable by `ErrorLevel` command. Maybe because `-1` isn't a value I expected for an error level, since this is no checkable with a classic `If ErrorLevel N`. So it implies the sample script from the linked answer doens"t check correctly the status. Also, I didn't find any documentation about exit statuses of InstallUtil. – Amessihel Jun 30 '20 at 08:49
  • 1
    It's definitely weird to see an errorlevel of -1, so it's not surprising that it caused you trouble. – SomethingDark Jun 30 '20 at 09:42