Windows Server 2003 上的错误级别始终返回 0

Windows Server 2003 上的错误级别始终返回 0

我正在使用的批处理文件:

@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%

在 Windows Server 2003(32 位和 64 位)上,我得到:

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>

但在 Windows Server 2008 上我得到:

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>

注意区别:

errorlevel = 0
errorlevel = 1060

哦为什么??


答案1

2003 年和 2008 年的 sc.exe 版本和文件大小有很大不同。程序是否返回命令解释器的代码以及返回什么代码完全取决于代码编写者。我敢肯定他们在 2008 年修复了代码。毕竟,sc.exe 命令本身仍能成功完成,因此返回代码 0 在技术上是有效的,尽管用处不大。

这个人也证实了我的故事,即 2003/XP 时代的 sc.exe 不会返回好的代码:http://waynes-world-it.blogspot.com/2008/12/command-line-automation-errorlevels-and.html

之前在 SF 上也讨论过这个问题:Windows 命令 SC,如何检查错误级别

最后一点修改 - 您至少可以将 sc 的输出通过管道传输到 find - 如果 find 未找到诸如“RUNNING”之类的字符串,则 Errorlevel 将升至 1。否则为 0。这至少是件好事。

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

相关内容