如何检查从批处理脚本服务的启动类型? (在视窗7)(How to check service s

2019-10-18 18:32发布

我需要(使用开始从批处理文件服务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和停止/命令行启动它。

我缺少的东西吗? 有没有更好的办法做到这一点?

Answer 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,所以我想这默认的内存分配是不够的。

现在,因为我基本上欠计算器我的事业,我会后我完整的代码:)

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


文章来源: How to check service startup type from batch script? (in windows 7)