theniteowl
Programmer
I am looking to retrieve the status of a service.
I have the code below but wonder if it could be written better. Is there an easier way to retrieve the status for a single service than returning a recordset of one and then looping through it? Seems silly to have to loop through when I KNOW I am only checking a single service.
Here is my current code. This is modified from a script I found on the web for toggling a service off then on again.
At my age I still learn something new every day, but I forget two others.
I have the code below but wonder if it could be written better. Is there an easier way to retrieve the status for a single service than returning a recordset of one and then looping through it? Seems silly to have to loop through when I KNOW I am only checking a single service.
Here is my current code. This is modified from a script I found on the web for toggling a service off then on again.
Code:
Option Explicit
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService
strComputer = "."
'On Error Resume Next
strService = " 'Alerter' "
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name ="_
& strService & " ")
For Each objService in colListOfServices
WScript.Echo objService.State
Next
WScript.Quit
At my age I still learn something new every day, but I forget two others.