errorlevel returns always 0 on Windows Server 2003 [closed]

Posted on

errorlevel returns always 0 on Windows Server 2003 [closed] – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about windows, , , , .

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:WINDOWSsystem32lsass.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:WINDOWSsystem32lsass.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??


Solution :

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

Leave a Reply

Your email address will not be published.