NSIS检查Windows服务的状态

E-M*_*Max 3 service nsis

我正在编写NSIS脚本,我需要检查服务状态(运行/停止/暂停/不存在),然后进行一些操作.但我不能使用任何用户库,如nsSCM.

我找到了一个脚本

sc QUERY ServiceNameHere | FIND "RUNNING"

但是我找不到如何在NSIS脚本中检查返回结果.

请帮忙.

Kyl*_*net 7

如果您可以使用插件:

使用Simple Service Plugin,您可以执行以下操作:

SimpleSC::GetServiceStatus "MyService"
Pop $0 ; returns an errorcode (!=0) otherwise success (0)
Pop $1 ; return the status of the service (see below)
Run Code Online (Sandbox Code Playgroud)

如果成功,服务状态将具有以下数值之一:

  1. 已停止
  2. START_PENDING
  3. STOP_PENDING
  4. RUNNING
  5. CONTINUE_PENDING
  6. PAUSE_PENDING
  7. PAUSED

如果你不能使用插件:

请注意,我将/ C添加到FIND.exe以输出行数而不是整行.另外,小心修改引号.为了做到这一点需要一些反复试验.

StrCpy $R0 '"$SYSDIR\cmd.exe" /c "sc QUERY MyServiceName | FIND /C "RUNNING""'
nsExec::ExecToStack '$R0'
Pop $R1  # contains return code
Pop $R2  # contains output
${If} $R1 == "0"    
    # command success
    ${If} $R2 == "1"
        # it's running
    ${Else}
        # it's not running
    ${EndIf}
${Else}
    # command failed
${EndIf}
Run Code Online (Sandbox Code Playgroud)

确保包含逻辑库,因为NSIS要求条件语句宏:

# Included files
!include LogicLib.nsh
Run Code Online (Sandbox Code Playgroud)