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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if an item exists in a collection from a WMI query? 2

Status
Not open for further replies.

shinedog

MIS
Feb 24, 2004
60
US
Hey all. What I'm trying to accomplish is a basic script that will query a server for 4 services. If the service is installed, it will query the service for certain properties. I'm basically doing something like:

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE DisplayName='Automatic Updates' or DisplayName='DNS Client' or DisplayName='Something Else' or Displayname='Yet Another Service'")

I have no problem iterating though the collection to get the properties that I want if the service is installed. What I'm having difficulties with is comprehending a way to see what services of the 4 I am attempting to add to the collection actually made it to the collection. So if I query for 4 and the count ends up being 2, how can I tell which of the 4 didn't get added to the collection? Sure I can assume that if A and C exist, then B and D aren't installed but I'm trying to write this info to a CSV and would like to write "Service B does not exist" to the file rather than not having any notation at all for that service and using a For/Next to go through the collection won't tell me what's not there.
 
Check the DisplayName property of the item ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Well yeah I would be checking the DisplayName but how to compare that to what is in the collection is my problem. A collection doesn't have a .Exists for it. I'm basically looking for a way to say "is 'DNS Client' in oCollection.
 
A starting point:
boolExists = False
strService = "'DNS Client"
For Each objItem In colItems
If objItem.DisplayName = strService Then
boolExists = True
Exit For
End If
Next
MsgBox "IsExists(" & strService & ") = " & boolExists

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I tried different variations of the code from PHV above but that gives me the same results basically. It gives me what services made it into the collection but not what services didn't.

I ended up kludging the code shown below together. I used the array list functionality from .NET as array list manipulation is ten times easier than the half-assed array functionality. Granted that means you need the .NET framework installed on any station that executes this code.

'create a .NET arraylist of the services I am looking for
Set arrServices = CreateObject( "System.Collections.ArrayList" )
arrServices.Add "IIS Admin"
arrServices.Add "World Wide Web Publishing"
arrServices.Add "Something Else"
arrServices.Add "Yet Another Service"

'set up WMI connection
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
'build a collection of the target services based on the contents of the array list
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE DisplayName='" & arrServices(0) & "' or DisplayName='" & arrServices(1) & "' or DisplayName='" & arrServices(2) & "' or DisplayName='" & arrServices(3) & "'") 'look for services

WScript.Echo "The following services are installed:"

'iterate through the collection
For Each item In colItems
'execute code here on items that made it into the collection
WScript.Echo item.DisplayName
'remove items that made it into the collection from the array list
arrServices.Remove(item.DisplayName)
Next

WScript.Echo "The following services were not found!"

'iterate through what's left in the array list to find what services were not found
For Each service In arrServices
'write a line to a log file saying "service not found" or whatever
WScript.Echo service
Next
 
I tried using the Scripting.Dictionary but the key/value combination was a little complicated for what I was trying to do. Creating arbitrary unrelated keys just to reference the value for a particular service within the array was kind of a pain.
 
Looking at your previous code...I don't see why you can't just use the dictionary object to accomplish the same.

Code:
Option Explicit

Dim objDict : Set objDict = CreateObject("Scripting.Dictionary")
objDict.CompareMode = vbTextCompare
objDict.Add "Automatic Updates", ""
objDict.Add "Another Bogus Service", ""
objDict.Add "DNS Client", ""
objDict.Add "Bogus Service Name", ""

Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Dim colServices : Set colServices = objWMIService.ExecQuery("SELECT DisplayName FROM Win32_Service")
Dim objService
For Each objService In colServices
	If objDict.Exists(objService.DisplayName) Then 
		objDict.Remove(objService.DisplayName)
	End If
Next

WScript.Echo "The following services could not be found:" & _
			 VbCrLf & Join(objDict.Keys, VbCrLf)

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Interesting. When I was playing around with Scripting.Dictionary, I was thinking something like:

objDict.Add "a", "Automatic Updates"
objDict.Add "b". "Another Bogus Service"
objDict.Add "c", "DNS Client"
objDict.Add "d", "Bogus Service Name"

I didn't really think to use the service name as the key and leave the values blank. K.I.S.S. indeed! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top