I have the following simple script:

--- begin -----
Set wmi=GetObject("winmgmts:{impersonationLevel=impersonate}!root/wmi")
Set ws=wmi.ExecQuery("select * from MSNdis_80211_BSSIList")
On Error Resume Next
For each wa in ws
WScript.Echo "Adapter name=", wa.InstanceName
Next
Wscript.Echo "after query:" & Err.Number
---- end ---------

This works fine if there is at least one wireless adapter.
If there are NO wireless adapters and I omit "On Error Resume Next",
an error occurs: my.vbs(6, 1) (null): 0x8004100C
The error is on the line of "For each wa in ws".
With "On Error Resume Next", the script runs to the end
and Err.Number is 424 (Object required).
I don't understand what this error means and why it occurs.
If the query returns an empty set, the For loop should not execute at all,
correct?
When there are one or more wireless adapters, the loop works fine, no error.
So, instead of empty set, the query returned something bad to ws?

I'd like to distinguish the empty set from error. I tried to
test IsEmpty(ws) or IsNull(ws) before the For loop -
both tests return false (that's, not empty and not null)

Is this a WMI or vbscript problem, or a hole in my head?

Re: How to check for empty collection? by axtens

axtens
Sun Oct 15 07:33:59 CDT 2006

I had thought that
<http://www.microsoft.com/technet/scriptcenter/guide/sas_wmi_srzu.mspx?mfr=true>
might give me a clue. Which it did, but the clue didn't work. I tried

Set wmi=GetObject("winmgmts:{impersonationLevel=impersonate}!root/wmi")
Set ws=wmi.ExecQuery("select * from MSNdis_80211_BSSIList")
if ws.Count <> 0 Then
For each wa in ws
WScript.Echo "Adapter name=", wa.InstanceName
Next
Else
wscript.echo "No adapters."
End If

However, I got a "SWbemObjectSet: Not supported" error. Hmmm ... will
have to have a deeper look at this one.

Kind regards,
Bruce M. Axtens
Internal Engineer
Strapper Technologies


Re: How to check for empty collection? by pavel_a

pavel_a
Sun Oct 15 08:01:01 CDT 2006

Thanks. I've tried "ws.Count" and it behaves in the same weird way.
When the query result is not empty, Count is the actual number of items.
But when there are no instances, Count appears to be "null" or whatever
it is in vbscript: WScript.Echo "count=", ws.Count
does not print anything at all.

So my question is: should WMI return an object that
can be tested by IsNull or IsEmpty, instead of trial and error ?
Or someting is wrong in my vbscript snippet?

the OS is XP SP2 with wsh 5.6 + usual stuff (vs2005, office...)

Regards,
--PA

"axtens" wrote:
> I had thought that
> <http://www.microsoft.com/technet/scriptcenter/guide/sas_wmi_srzu.mspx?mfr=true>
> might give me a clue. Which it did, but the clue didn't work. I tried
>
> Set wmi=GetObject("winmgmts:{impersonationLevel=impersonate}!root/wmi")
> Set ws=wmi.ExecQuery("select * from MSNdis_80211_BSSIList")
> if ws.Count <> 0 Then
> For each wa in ws
> WScript.Echo "Adapter name=", wa.InstanceName
> Next
> Else
> wscript.echo "No adapters."
> End If
>
> However, I got a "SWbemObjectSet: Not supported" error. Hmmm ... will
> have to have a deeper look at this one.
>
> Kind regards,
> Bruce M. Axtens
> Internal Engineer
> Strapper Technologies
>
>

Re: How to check for empty collection? by Richard

Richard
Sun Oct 15 11:53:33 CDT 2006

Looks like a bug to me. The only solution I can find is to use On Error
Resume Next. This encourages people to use On Error Resume Next throughout.
I note that IsArray is never True. The ws object is a collection.

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net

"Pavel A." <pavel_a@NOwritemeNO.com> wrote in message
news:FCAE07E0-6C2C-47E2-B203-B2D043A12F97@microsoft.com...
> Thanks. I've tried "ws.Count" and it behaves in the same weird way.
> When the query result is not empty, Count is the actual number of items.
> But when there are no instances, Count appears to be "null" or whatever
> it is in vbscript: WScript.Echo "count=", ws.Count
> does not print anything at all.
>
> So my question is: should WMI return an object that
> can be tested by IsNull or IsEmpty, instead of trial and error ?
> Or someting is wrong in my vbscript snippet?
>
> the OS is XP SP2 with wsh 5.6 + usual stuff (vs2005, office...)
>
> Regards,
> --PA
>
> "axtens" wrote:
>> I had thought that
>> <http://www.microsoft.com/technet/scriptcenter/guide/sas_wmi_srzu.mspx?mfr=true>
>> might give me a clue. Which it did, but the clue didn't work. I tried
>>
>> Set wmi=GetObject("winmgmts:{impersonationLevel=impersonate}!root/wmi")
>> Set ws=wmi.ExecQuery("select * from MSNdis_80211_BSSIList")
>> if ws.Count <> 0 Then
>> For each wa in ws
>> WScript.Echo "Adapter name=", wa.InstanceName
>> Next
>> Else
>> wscript.echo "No adapters."
>> End If
>>
>> However, I got a "SWbemObjectSet: Not supported" error. Hmmm ... will
>> have to have a deeper look at this one.
>>
>> Kind regards,
>> Bruce M. Axtens
>> Internal Engineer
>> Strapper Technologies
>>
>>



Re: How to check for empty collection? by axtens

axtens
Mon Oct 16 08:10:14 CDT 2006

There may be another way into this. A bit of digging uncovered MS
talking about wbemdisp.tlb
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/swbemobjectset_count.asp>
and
<http://windowssdk.msdn.microsoft.com/en-us/library/ms750589(VS.80).aspx>.


Desert Hill's Objectscope says, of wbemdisp.tlb:
Library WbemScripting v1.2
Microsoft WMI Scripting V1.2 Library
C:\WINDOWS\system32\wbem\wbemdisp.tlb
{565783C6-CB41-11D1-8B02-00600806D9B6}

I'm not at all sure how, but there may be a way to achieve what you
want though possibly at the cost of stepping out of VBScript into VB or
Delphi.

Kind regards,
Bruce M. Axtens
Internal Engineer
Strapper Technologies