Previous Section Table of Contents Next Section

The All-Purpose WMI Query Script

About 75% of my time with WMI is spent querying information. Fortunately, there's a very simple template you can use. In fact, this is identical to the code produced by PrimalScript's WMI Query Wizard and by the Microsoft Scriptomatic tool. The fact that those tools exist goes to show how generic and all-purpose WMI scripting really can be.

Start by finding the WMI class that you need to query. This isn't hard, but it can be time-consuming because there are so many classes to choose from. I usually use PrimalScript or the Scriptomatic to browse through the classes until I see one I want.

Next, define the three variables you need to query WMI.


Dim strComputer

Dim objWMIService

Dim colItems

Then, write the actual query.


strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & _

 strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery( _

 "Select * from class_name_here",,48)

Notice that you can change the value assigned to strComputer to query a remote machine. Insert the appropriate class name. Next, write a For Each…Next loop that iterates through the classes that you queried.


For Each objItem in colItems

Next

Finally, insert the appropriate lines within the construct to work with the class' properties.


WScript.Echo objItem.property_name_here

WScript.Echo objItem.property_name_here

If you don't want to display the information, write it to a text file, assign it to a variable, or do whatever you like. For example, let's say you want to limit the number of class instances returned by your query. When querying Win32_QuickFixEngineering, you receive all installed hot fixes. What if you just want a particular one? No problem. Modify your query appropriately.


strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & _

 strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery( _

 "Select * from Win32_QuickFixEngineering " & _

 "WHERE HotFixID = 'Q123456'", _

 ,48)

You can specify a WHERE clause and indicate any property you want, listing the desired value for that property. WMI obeys, and returns only the instances that match your query. Pretty simple! This all-purpose query template can be adapted to almost any purpose. For example, the following script lists various properties of a network adapter configuration. The bold faced elements are the class and property names I plugged into the generic template.


Dim strComputer

Dim objWMIService

Dim colItems



strComputer = "."

Set objWMIService = GetObject( _

 "winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery( _

 "Select * from Win32_NetworkAdapterConfiguration",,48)



For Each objItem in colItems

      WScript.Echo "ArpAlwaysSourceRoute: " & objItem.ArpAlwaysSourceRoute

      WScript.Echo "ArpUseEtherSNAP: " & objItem.ArpUseEtherSNAP

      WScript.Echo "Caption: " & objItem.Caption

      WScript.Echo "DatabasePath: " & objItem.DatabasePath

      WScript.Echo "DeadGWDetectEnabled: " & _

       objItem.DeadGWDetectEnabled

      WScript.Echo "DefaultIPGateway: " & _

       objItem.DefaultIPGateway

      WScript.Echo "DefaultTOS: " & objItem.DefaultTOS

      WScript.Echo "DefaultTTL: " & objItem.DefaultTTL

      WScript.Echo "Description: " & objItem.Description

      WScript.Echo "DHCPEnabled: " & objItem.DHCPEnabled

      WScript.Echo "DHCPLeaseExpires: " & _

       objItem.DHCPLeaseExpires

      WScript.Echo "DHCPLeaseObtained: " & _

        objItem.DHCPLeaseObtained

Next

This template makes a nice, easy way to work with just about any type of WMI query you need to write.

    Previous Section Table of Contents Next Section