how to check service state using vbscript?

╄→尐↘猪︶ㄣ 提交于 2021-02-07 13:57:15

问题


Is there anyway to check the status of service using vbscript ? I would like to have a function for each possible service state : LINK Any help would be great. I did write a function for checking if service is stopped :

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

回答1:


When in doubt, read the documentation. All you need to do is check the State property of the service object:

serviceName = "..."

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



回答2:


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



回答3:


' 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 

Here is the source



来源:https://stackoverflow.com/questions/21074223/how-to-check-service-state-using-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!