好的,我刚刚想通了。
首先,我必须道歉,因为原来的错误实际上是:
[SC] QueryServiceConfig FAILED 122:
The data area passed to a system call is too small.
[SC] GetServiceConfig needs 718 bytes
并不是
[SC] OpenService FAILED 1060:
正如我第一次说的。
显然,我必须明确地为我的服务添加一个缓冲区大小:sc qc XXX 1000
之后,我注意到 BINARY_PATH_NAME 字段对于 XXX 来说非常长,所以我猜默认内存分配是不够的。
现在,由于我的职业生涯基本上欠 StackOverflow,我将发布我的完整代码 :)
rem start a service, but only if it is configured as automatic, and only if it isn't running already
for /F "tokens=3 delims=: " %%H in ('sc qc %xxx% 1000^| findstr "START_TYPE"') do (
if /I "%%H" EQU "AUTO_START" (
rem check if service is stopped
for /F "tokens=3 delims=: " %%H in ('sc query %xxx% ^| findstr "STATE"') do (
if /I "%%H" EQU "STOPPED" (
echo net start %xxx%
net start %xxx%
) else (
echo %xxx% is already running
)
)
) else (
echo Skipping %xxx% since it's not defined as automatic start
)
)