我想使用VBScript来检查后台处理程序服务是否已启动,如果没有启动它,下面的代码会检查服务状态,但我需要一些帮助来修改它,以便检查它是否已启动。
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
("Select * from Win32_Service")
For Each objService in colRunningServices
Wscript.Echo objService.DisplayName & VbTab & objService.State
Next
非常感谢 史蒂芬
答案 0 :(得分:5)
这样的事情怎么样?如果它尚未运行,则此命令将启动它。无需提前检查。
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run "NET START spooler", 1, false
答案 1 :(得分:1)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
("select State from Win32_Service where Name = 'Spooler'")
For Each objService in colRunningServices
If objService.State <> "Running" Then
errReturn = objService.StartService()
End If
Next
请注意,您也可以使用objService.started
来检查它是否已启动。
答案 2 :(得分:0)
只是为了完全没有,这是使用Shell.Application
对象的替代变体:
Const strServiceName = "Spooler"
Set oShell = CreateObject("Shell.Application")
If Not oShell.IsServiceRunning(strServiceName) Then
oShell.ServiceStart strServiceName, False
End If
或者简单地说:
Set oShell = CreateObject("Shell.Application")
oShell.ServiceStart "Spooler", False ''# This returns False if the service is already running