SWbemObjectSet.ItemIndex method

The ItemIndex method returns the SWbemObject associated with the specified index into the collection. The index indicates the position of the element within the collection. Collection numbering starts at zero.

Syntax

objWbemObject = .ItemIndex( _
  ByVal lIndex _
)

Parameters

lIndex

Index of the item in the collection.

Return value

If successful, the requested SWbemObject object returns.

Error codes

Upon completion of the Item method, the Err object may contain one of the error codes below.

wbemErrFailed - 2147749889 (0x80041001)

Unspecified error.

wbemErrInvalidParameter - 2147749896 (0x80041008)

Invalid parameter was specified. This error is returned if a negative index number is supplied.

wbemErrOutOfMemory - 2147749894 (0x80041006)

Not enough memory to complete the operation.

wbemErrNotFound - 2147749890 (0x80041002)

Requested item was not found.

Remarks

The ItemIndex method allows WMI clients scripts and applications written in any language a uniform way to manipulate a collection like an array. This method can be used with SWbemObjectSet collections. Queries, such as SWbemServices.AssociatorsOf, SWbemServices.ReferencesTo, SWbemServices.InstancesOf, or SWbemServices.ExecQuery return SWbemObjectSet collections. The ItemIndex method does not work with collections which do not contain SWbemObjects, such as SWbemMethodSet, SWbemNamedValueSet, SWbemPrivilegeSet, SWbemPropertySet, and SWbemQualifierSet.

ItemIndex can also be used to obtain the single instance of a singleton class.

Examples

The following VBScript code example queries for a collection of all the Win32_Process instances then displays the names of the first three processes.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & _
    strComputer & "\root\cimv2")

set colProcesses = _
    objWMIService.Execquery("Select * from Win32_Process")
Wscript.Echo  colProcesses.ItemIndex(0).Name
Wscript.Echo  colProcesses.ItemIndex(1).Name
Wscript.Echo  colProcesses.ItemIndex(2).Name

Only one instance of Win32_OperatingSystem exists for each operating system installation. Creating the GetObject path to obtain the single instance is awkward so scripts normally enumerate Win32_OperatingSystem even though only one instance is available. The following VBScript code example shows how to use the ItemIndex method to get to the one Win32_OperatingSystem without using a For Each loop.

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

Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")

Wscript.Echo "Caption: " & colOperatingSystems.ItemIndex(0).Caption

The following VBScript code example gets instances associated with Win32_OperatingSystem, such as Win32_SystemOperatingSystem.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & _
    strComputer & "\root\cimv2")

set colOS = _
    objWMIService.Execquery("Select * from Win32_OperatingSystem")
    Wscript.Echo  colOS.ItemIndex(0).Name

set colAssociators = colOS.ItemIndex(0).Associators_
    For Each Associator in colAssociators 
        Wscript.Echo Associator.Path_.RelPath  
    Next

The following code example output shows instances associated with Win32_OperatingSystem.

Windows Server 2008 
    |C:\Windows|\Device\Harddisk0\Partition1
Win32_ComputerSystem.Name="Test1"
Win32_AutochkSetting.SettingID="Windows Server 2008 
    |C:\\Windows|\\Device\\Harddisk0\\Partition1"

Requirements

Requirement Value
Minimum supported client
Windows Vista
Minimum supported server
Windows Server 2008
Header
Wbemdisp.h
Type library
Wbemdisp.tlb
DLL
Wbemdisp.dll
CLSID
CLSID_SWbemObjectSet
IID
IID_ISWbemObjectSet

See also

SWbemObjectSet