Service NSIS检查Windows服务的状态
我正在编写NSIS脚本,需要检查服务状态(运行/停止/暂停/不存在),然后执行一些操作。 但是我不能使用任何用户库,比如nsSCM 我找到了剧本Service NSIS检查Windows服务的状态,service,nsis,Service,Nsis,我正在编写NSIS脚本,需要检查服务状态(运行/停止/暂停/不存在),然后执行一些操作。 但是我不能使用任何用户库,比如nsSCM 我找到了剧本 sc查询服务名称此处|查找“正在运行” 但是我找不到如何在NSIS脚本中检查返回结果 请提供帮助。如果您可以使用插件: 使用,您可以执行以下操作: SimpleSC::GetServiceStatus "MyService" Pop $0 ; returns an errorcode (!=0) otherwise success (0) Pop $1
sc查询服务名称此处|查找“正在运行”
但是我找不到如何在NSIS脚本中检查返回结果
请提供帮助。如果您可以使用插件: 使用,您可以执行以下操作:
SimpleSC::GetServiceStatus "MyService"
Pop $0 ; returns an errorcode (!=0) otherwise success (0)
Pop $1 ; return the status of the service (see below)
如果成功,服务状态将具有以下数值之一:
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}
请确保包含逻辑库,因为NSIS对条件语句宏要求:
# Included files
!include LogicLib.nsh
有几个NSIS插件和助手函数处理NT服务:,和。wiki概述了以下内容 使用sc.exe是有问题的,因为输出可能是本地化的,net.exe可能更好(它也存在于
!include LogicLib.nsh
StrCpy $1 "Event Log" ;Put your service name here
ExpandEnvStrings $0 "%comspec%"
nsExec::ExecToStack '"$0" /k "net start | FIND /C /I "$1""'
Pop $0
Pop $1
StrCpy $1 $1 1
${If} "$0$1" == "01"
MessageBox mb_ok "Running"
${Else}
MessageBox mb_ok "Not Running"
${EndIf}
我通过使用其显示名称(而不是服务名称)检查服务是否正在运行,因为它往往更精确(例如,服务名称是JETTY,而显示名称使用我的产品名称-我避免了计算由另一个产品安装的JETTY服务的风险) 因此,根据Kyle的解决方案,我使用:
var running
!macro CheckMyService
StrCpy $running "0"
StrCpy $cmd '"$SYSDIR\cmd.exe" /c "net start | FIND /C "MyServiceDisplayName""'
nsExec::ExecToStack '$cmd'
Pop $R1 # contains return code
Pop $R2 # contains output
StrCpy $n $R2 1
${If} $R1 == "0"
${If} $n == "1"
StrCpy $running "1"
${EndIf}
${EndIf}
DetailPrint "runnning(1=yes): $running"
!macroend
您可以不使用任何插件,或者不使用nsSCM吗?因为在NSIS中使用插件会让生活变得更轻松。我可以
t使用插件,因为我需要将此脚本提供给其他人,他们根本就没有t插件