Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check service state using vbscript?

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
like image 962
Anh Nguyen Avatar asked Jan 12 '14 11:01

Anh Nguyen


2 Answers

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
like image 168
Ansgar Wiechers Avatar answered Sep 20 '22 12:09

Ansgar Wiechers


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
like image 27
arvind123 Avatar answered Sep 21 '22 12:09

arvind123