Enumeration is the act of moving through a set of objects and possibly modifying each object as you do so. For example, you can enumerate through a set of Win32_DiskDrive objects to find a particular serial number. Note that although you can enumerate any object, WMI only returns objects to which you have security access.
Enumerations of large data sets can require a large amount of resources and degrade performance. For more information, see Improving Enumeration Performance. You also can request data with a more specific query. For more information, see Querying WMI.
The following sections are discussed in this topic:
If you do not know the object path for a specific instance, or you want to retrieve all the instances for a specific class, use the SWbemServices.InstancesOf method to return an SWbemObjectSet enumeration of all the instances of a class. Alternatively you can query WMI through SWbemServices.ExecQuery to obtain the same set of objects.
The following procedure describes how to enumerate the instances of a class using VBScript.
To enumerate the instances of a class using VBScript
- Enumerate the instances with a call to the
The InstancesOf method returns a collection, or set, of objects through which you can enumerate. For more information, see Accessing a Collection. The returned collection is actually an SWbemObjectSet object, so any of the methods of that object can be called.
If you wish to retrieve a WMI class instance in another namespace or on a different computer, specify the computer and namespace in the moniker. For more information, see Creating a WMI Script. This only works if you have the appropriate access privileges. For more information, see Maintaining WMI Security and Executing Privileged Operations.
- Retrieve any individual instances you wish using the collections methods.
Set objCol = GetObject("WinMgmts:").InstancesOf("Win32_LogicalDisk") For Each Drive In objCol If Not IsNull(Drive.Size) Then WScript.Echo ("Drive " & Drive.deviceid & " contains " _ & Drive.Size & " bytes") Else WScript.Echo ("Drive " & Drive.deviceid __ & " is not available.") End If Next
In addition to performing basic enumeration, you can set several flags and properties to increase the performance of your enumeration. For more information, see Improving Enumeration Performance.
To enumerate an object set in WMI
- Create an
IEnumWbemClassObject interface describing the set of objects you wish to enumerate.
An IEnumWbemClassObject object contains a list describing a set of WMI objects. You can use the IEnumWbemClassObject methods to enumerate forwards, skip objects, begin at the beginning, and copy the enumerator. The following table lists the methods use to create enumerators for different types of WMI objects.
Object Method Class Instance Query result Event notification
- Traverse through the returned enumeration using multiple calls to IEnumWbemClassObject::Next or IEnumWbemClassObject::NextAsync.
For more information, see Manipulating Class and Instance Information.