Is it possible to pull a single item out of a WMI query collection?
I am trying to pull a single item out of a collection that is returned from a WMI query. Example:

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colInterfaces = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

interface = colInterfaces.Item(0)
ipAddress = interface.ipAddress(0)

However I am getting a generic failure on the "colInterfaces.Item(0)" line. Is this method of doing things supported, or must I always use the "For Each" method with the collection that is returned from WMI queries?
October 13th, 2009 2:53pm

If I understand your question...in VBScript, you cannot get the first item from a collection without using For Each:

For Each Item in CollectionObject
  Set Result = Item
  Exit For
Next

It's easier in JScript:

var enum = new Enumerator(collectionObject);
var result = enum.item();

Bill
Free Windows Admin Tool Kit Click here and download it now
October 13th, 2009 2:56pm

As Bill said, you can't use an integer index to enumerate SWbemObjectSet or access its member (see here) - you need to provide the relative path to the object you want to access. This means you have to know the class name and values of key properties of the object. Win32_NetworkAdapterConfiguration key property is 'Index' , so to access a single element you need to use this:


Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colInterfaces = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration " _
    & "Where IPEnabled = True")

Set interface = colInterfaces _
    ("Win32_NetworkAdapterConfiguration.Index=1")

ipAddress = interface.ipAddress(0)

WScript.Echo ipaddress


I had to look up the value of Index. If you know key property values, you can also use SWbemServices.Get:



Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set interface = objWMIService.Get _
    ("Win32_NetworkAdapterConfiguration.Index=1")

ipAddress = interface.ipAddress(0)

WScript.Echo ipaddress


But most of the time you will query WMI using properties that are not keys (like IPEnabled), and in that case the the easiest solution is to use For..Each, even if your SWbemObjectSet has only one member.
October 13th, 2009 4:05pm

Use ItemIndex instead of Item

This is described in the article linked in Uros Calakovic's post.

e.g:

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colInterfaces = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
Set interface = colInterfaces.ItemIndex(0)
ipAddress = interface.ipAddress(0)
Wscript.Echo IpAddress

Given this obvious answer has not been already mentioned, maybe it's new with Windows 7?

Free Windows Admin Tool Kit Click here and download it now
August 18th, 2015 10:10pm

Use ItemIndex instead of Item

This is described in the article linked in Uros Calakovic's post.

e.g:

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colInterfaces = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
Set interface = colInterfaces.ItemIndex(0)
ipAddress = interface.ipAddress(0)
Wscript.Echo IpAddress

Given this obvious answer has not been already mentioned, maybe it's new with Windows 7?

August 18th, 2015 10:38pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics