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?
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
- Proposed as answer by Bill_StewartModerator Wednesday, October 21, 2009 5:12 PM
- Marked as answer by IamMredMicrosoft employee, Owner Friday, January 08, 2010 6:53 AM
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.
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?
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 IpAddressGiven this obvious answer has not been already mentioned, maybe it's new with Windows 7?