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

WMI query....capture empty collections?

Status
Not open for further replies.

VulcanJedi

Technical User
Oct 2, 2002
430
US
I have the following lines of code:
the issue is when colleggedEvents is empty and in nothing is returned from the query than the for each loop crashes; is there a way to capture if the query is empty??


Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'Application' and " _
& "EventCode = '1106' and Type = 'error'")

For Each objevent In colLoggedEvents

[yinyang] Tranpkp [pc2]
 
I would look at IsObject() or IsEmpty().

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Nope and NOpe?
I could make the original query less restrictve and the code to search but i'd rather have the query more pithy when pulling information from remote pcs and presume the performance faster working w/ less data.
There has to be a method of trapping for non existant/empty collection ....anyone???

[yinyang] Tranpkp [pc2]
 
If colLoggedEvents.Count = 0 Then [whatever you want: exit, display something, go the next computer]
 
While i'm appreciative of all those willing to hlep; I'm tired of just trying all these things ppl are guessing on.
I've tried isarray, isnumeric, inobject, isnull, isempty, colloggedevents=0...none of these work.

[yinyang] Tranpkp [pc2]
 
Try this instead.
[tt]
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate[red],(Security)[/red]}!\\" & strComputer & "\root\cimv2")

Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'Application' and " _
& "EventCode = 1106 and Type = [red]1[/red]")
[/tt]
 
Thanks; but that didnt work either....I had to encapsulate the for each loop within on error resume next // on error goto 0.
I still hope there is a better way out there but I've not found one.

[yinyang] Tranpkp [pc2]
 
The case where error can be raised at the line "for each ..." is not that it is empty, but, either you cannot receive the item (for instance, you have not enable the security privilege, or, you have not enough right to access strComputer's log...) or you query string is wrong. If both of these causes do not meet, empty collect just behaves normally, that is doing nothing.
 
recordsets somethings give back funny .count or some dont support it or blaa blaa.
i dont think i have found problems with WMI .counts, if you have satisfied tsuji response and the .count legitimately is not returning anything good then chuck a incrementing counter in the for each loop to get the real count....of 0 ;-)
 
I'm an admin on all the boxes. I am using on error resume next and incrementing a manual count to get past this but yeah until someone proves me wrong this seems like the best way now...


[yinyang] Tranpkp [pc2]
 
You do whatever you want on your boxes. My post just concern the cold matter of technology.
 
Easy there friend. I am stating fact I AM a local administrator. if I log in to that box I can see that I can access the log; which is how i can confirm the query would be empty. A MS MVP whos an excellent instructor / author replied on alternative forums that VBscript DOES have issues handling null collections.

[yinyang] Tranpkp [pc2]
 
I sent the forum two causes, not empty statement.
 
hmm, i am sure we are spliting hairs but i would say that 'tranpkp' has trouble handling null collections not 'vbscript' whoever they are, ;-)
 
mrmovie; i've been trying to find the solution or i wouldnt be here. If you assert my statments are false I would love for you to champion a valid solution.

[yinyang] Tranpkp [pc2]
 
i guess i was agreeing with tsuji.
i run your code on my machine it returns a collection with no members, vbscript does not have an issue with it,,,so, as tsuji suggests you problem is furthur up the chain

'the code on my machine is
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'Application' and " _
& "EventCode = '1106' and Type = 'error'")
Wscript.Echo colLoggedEvents.Count
For Each objevent In colLoggedEvents
Wscript.echo objEvent
Next
'i have no events in my application log matching the criteria, i get 0 echo'd, no errors etc, and i would summize that colLoggedEvents is??? an empty collection? or perhaps something else
 
>recordsets somethings give back funny .count or some dont support it or blaa blaa

Don't make the mistake of thinking that WQL, which happens to look rather like SQL, returns ADO recordsets. It doesn't, and therefore the issues with recordset count properties does not arise.

>VBscript DOES have issues handling null collections

I'm afraid your 'expert' is talking nonsense, or you have misunderstood what they were trying to tell you.

And since colLoggedEvents is not a Collection but a SWbemObjectSet it wouldn't matter if it VBScript did have problems with null collections.

I think what we need to see is your actual code, rather than your illustrative extract (since it seems only to be you that is having the problem)
 
strongm; nice post thx it seems u have the best handle. What is a Swbemobjectset? I'll have to get on other machine later to post code if you're actually interested...
Regards,
KT...

[yinyang] Tranpkp [pc2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top