Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving status of service

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
US
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.

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.
 
It is a cool app and may prove useful for other things but does not answer this particular question.
The closest the app comes is generating a script to list out all services on a specified device. To do this it executes a query returning a list of services.

My question is, since I only want the status of a single service, can this be accomplished without returning a whole recordset and looping through it's objects when I already know which object I want and that I will only have one object set. It seems redundant to write the code to loop through a recordset that should only have one item.

I am sure the performance is not an issue, it just seems to be sloppy to setup a loop for one item. There must be a way to address the one service directly.


At my age I still learn something new every day, but I forget two others.
 
This is how WMI works: you always get a collection (with 0, 1 or more members).

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top