0

The batch file I'm using:

@ECHO OFF

CMD /C EXIT 0
echo errorlevel = %ERRORLEVEL%
sc qc jee
echo errorlevel = %ERRORLEVEL%
sc start jee
echo errorlevel = %ERRORLEVEL%
sc stop jee
echo errorlevel = %ERRORLEVEL%
sc qc Netlogon
echo errorlevel = %ERRORLEVEL%

On Windows Server 2003 (32&64bit) I get:

D:\Temp>errorleveltest.bat
errorlevel = 0
[SC] OpenService FAILED 1060:

The specified service does not exist as an installed service.

errorlevel = 0
[SC] StartService: OpenService FAILED 1060:

The specified service does not exist as an installed service.

errorlevel = 0
[SC] OpenService FAILED 1060:

The specified service does not exist as an installed service.

errorlevel = 0
[SC] QueryServiceConfig SUCCESS

SERVICE_NAME: Netlogon
    TYPE               : 20  WIN32_SHARE_PROCESS
    START_TYPE         : 2   AUTO_START
    ERROR_CONTROL      : 1   NORMAL
    BINARY_PATH_NAME   : C:\WINDOWS\system32\lsass.exe
    LOAD_ORDER_GROUP   : MS_WindowsRemoteValidation
    TAG                : 0
    DISPLAY_NAME       : Net Logon
    DEPENDENCIES       : LanmanWorkstation
    SERVICE_START_NAME : LocalSystem


errorlevel = 0
D:\Temp>

But on Windows Server 2008 I get:

D:\Temp>errorleveltest.bat
errorlevel = 0
[SC] OpenService FAILED 1060:

The specified service does not exist as an installed service.

errorlevel = 1060
[SC] StartService: OpenService FAILED 1060:

The specified service does not exist as an installed service.

errorlevel = 1060
[SC] OpenService FAILED 1060:

The specified service does not exist as an installed service.

errorlevel = 1060
[SC] QueryServiceConfig SUCCESS

SERVICE_NAME: Netlogon
    TYPE               : 20  WIN32_SHARE_PROCESS
    START_TYPE         : 2   AUTO_START
    ERROR_CONTROL      : 1   NORMAL
    BINARY_PATH_NAME   : C:\WINDOWS\system32\lsass.exe
    LOAD_ORDER_GROUP   : MS_WindowsRemoteValidation
    TAG                : 0
    DISPLAY_NAME       : Net Logon
    DEPENDENCIES       : LanmanWorkstation
    SERVICE_START_NAME : LocalSystem


errorlevel = 0
D:\Temp>

Notice the difference:

errorlevel = 0
errorlevel = 1060

Oh why??


Chris S
  • 77,945
  • 11
  • 124
  • 216

1 Answers1

3

The version and file size of sc.exe is much different between 2003 and 2008. It's completely up to the writer of the code if and what return codes their program gives back to the command interpreter. My money's on that they just fixed their code in 2008. After all, the sc.exe command itself still completes successfully, making a return code of 0 technically valid, albeit not very useful.

This guy also corroborates my story that 2003/XP-era sc.exe does not return good codes: http://waynes-world-it.blogspot.com/2008/12/command-line-automation-errorlevels-and.html

It's also been talked about before here on SF: Windows Command SC, how to check errorlevel

One last edit - You could at least pipe the output of sc to find - if find does not find a string such as "RUNNING" the Errorlevel will be raised to 1. 0 otherwise. That's at least something.

http://ss64.com/nt/sc.html

Ryan Ries
  • 55,481
  • 10
  • 142
  • 199