Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Enumerating WMI

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:

Enumerating WMI Using VBScript

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.

Aa390386.wedge(en-us,VS.85).gifTo enumerate the instances of a class using VBScript

  1. Enumerate the instances with a call to the SWbemServices.InstancesOf method.

    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.

  2. Retrieve any individual instances you wish using the collections methods.

The following code example retrieves an SWbemServices object and then executes the InstancesOf method to display the size property for all instances of logical drives on the local computer.

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")
       WScript.Echo ("Drive " & Drive.deviceid __
          & " is not available.")
    End If

Enumerating WMI Using C++

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.

Aa390386.wedge(en-us,VS.85).gifTo enumerate an object set in WMI

  1. 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.


    Query result
    Event notification


  2. Traverse through the returned enumeration using multiple calls to IEnumWbemClassObject::Next or IEnumWbemClassObject::NextAsync.

For more information, see Manipulating Class and Instance Information.



© 2015 Microsoft