【问题标题】:How to check service startup type from batch script? (in windows 7)如何从批处理脚本检查服务启动类型? (在 Windows 7 中)
【发布时间】:2013-10-15 15:22:42
【问题描述】:

我需要从批处理文件启动服务(使用sc start XXX),但前提是它配置了自动启动类型。

我阅读了sc /? 的说明,并尝试先调用sc qc XXX 命令以查询其配置,然后在结果上使用findstr,但在sc qc XXX 命令之后出现以下错误:

[SC] QueryServiceConfig FAILED 122:

The data area passed to a system call is too small.

[SC] GetServiceConfig needs 718 bytes

指定的服务不作为已安装的服务存在。

这很奇怪,因为我可以调用 sc config XXX 并从命令行停止/启动它。

我错过了什么吗?有没有更好的方法?

【问题讨论】:

  • PowerShell 可能更适合这类事情。
  • 在标准shell中没有办法吗?

标签: windows batch-file windows-7


【解决方案1】:

好的,我刚刚想通了。

首先,我必须道歉,因为原来的错误实际上是:

[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
    )
)

【讨论】:

  • 请编辑您的原始问题以获得正确的错误消息,这将有助于其他面临此问题的人找到此问题。
猜你喜欢
  • 1970-01-01
  • 2017-08-07
  • 2012-05-30
  • 2011-08-25
  • 2014-11-15
  • 2014-12-26
  • 1970-01-01
  • 2010-11-15
  • 1970-01-01
相关资源
最近更新 更多