Service 如何使用vbscript检查服务状态?

Service 如何使用vbscript检查服务状态?,service,vbscript,wmi,status,wmi-query,Service,Vbscript,Wmi,Status,Wmi Query,是否仍然可以使用vbscript检查服务状态?我想为每个可能的服务状态提供一个函数: 任何帮助都会很好。我确实编写了一个函数来检查服务是否停止: Public Function IsServiceStop(ByVal serviceName) On Error Resume Next Dim objServices, service Set oWmiService = GetObject("winmgmts:\\.\root\cimv2") Set objServ

是否仍然可以使用vbscript检查服务状态?我想为每个可能的服务状态提供一个函数: 任何帮助都会很好。我确实编写了一个函数来检查服务是否停止:

Public Function IsServiceStop(ByVal serviceName)
    On Error Resume Next
    Dim objServices, service
    Set oWmiService = GetObject("winmgmts:\\.\root\cimv2")
    Set objServices = oWmiService.ExecQuery("Select * from Win32_Service where Name='" & serviceName & "'")
    For Each service In objServices
        IsServiceStop = (service.Started = False)
        Exit Function
    Next
    IsServiceStop = True
    On Error Goto 0
End Function
如有疑问,请阅读本手册。您只需检查服务对象的
状态
属性:

serviceName = "..."

Set wmi = GetObject("winmgmts://./root/cimv2")
state = wmi.Get("Win32_Service.Name='" & serviceName & "'").State
WScript.Echo state

以下是

WMI本身是一项服务。它可能不可用
Windows Management Instrumentation
@Azevedo虽然WMI服务确实可能没有运行,但如果是这样的话,那么根据我的经验,有太多东西不起作用,不值得担心。
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")

For Each objService in colListOfServices

   status = objService.State

Next

Reporter.ReportEvent micPass, "startService", "Service status " & status
' Michael Maher 
' 3/10/07 
' Checks if services exists and is running 

strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name='Messenger'")  
nItems = colRunningServices.Count  

' If the collection count is greater than zero the service will exist. 

If nItems > 0  Then 

For Each objItem in colRunningServices 

If objItem.State = "Stopped" Then 
Wscript.Echo objItem.DisplayName & " Installed/Stopped" 
ElseIf objItem.State = "Started" Then 
Wscript.Echo objItem.DisplayName & " Installed/Running" 
End If 
Next 

Else 
Wscript.Echo "Service Not Installed" 
End If