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

我正在使用的batch file:

@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 

哦为什么??


sc.exe的版本和文件大小在2003年和2008年之间有很大的不同。完全取决于代码的编写者,如果他们的程序返回给命令解释器,返回代码是什么。 我的钱,他们只是在2008年修复他们的代码。毕竟,sc.exe命令本身仍然成功完成,使技术上有效的返回码0,尽pipe不是很有用。

这家伙还corroborates我的故事,2003 / XP时代sc.exe不会返回良好的代码: http : //waynes-world-it.blogspot.com/2008/12/command-line-automation-errorlevels-and.html

在这之前也曾经谈到SF: Windows命令SC,如何检查错误级别

最后一个编辑 – 你可以至less输出sc的输出来查找 – 如果find找不到string,如“RUNNING”,Errorlevel将被提升到1. 0,否则。 这至less是一些东西。

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