Scripting Solutions with WSH and COM: Use WMI to Query Machines for Relevant Information


This month, I had planned to continue the series "Using WSC to Build a Progress Bar Dialog Box" but decided instead to share with you a helpful script that I recently created. I needed to query the local computer to identify and display the IRQs and I/O port ranges of all the serial ports. Ordinarily, I would use a graphical management tool to query the registry for such information. This time, however, I decided to use Windows Management Instrumentation (WMI) because writing a WMI script to query the local machine is easy. In addition, I knew that after I wrote this basic script, I could later expand it to query any machine or all the machines in my network. Thus, I would have a powerful script for systems administration tasks.

The Basic Script
Listing 1 contains the basic script called QueryPorts.wsf. Here's how I went about writing this script. I began by including the necessary XML elements and variable declarations. (If you're unfamiliar with the XML elements, see "Scripting Solutions with WSH and COM: Using WSC to Build a Progress Bar Dialog Box, Part 1," September 2000.) I then created the column headings for the report by interspersing the strings "Serial Port", "Interrupt", and "I/O Port" with tabs (vbTab), concatenating them in the str variable.

Next, I used VBScript's GetObject function with WMI's programmatic identifier (ProgID) WinMgmts: to create an instance of the SWbemServices object and set that instance to the wmiObj variable. The SWbemServices object's methods and properties let me first retrieve the serial ports' device IDs, then use the device IDs to retrieve the serial ports' IRQs and I/O ports.

To retrieve the serial ports' device IDs (e.g., COM1, COM2), I used the SWbemServices::InstancesOf method with the Win32_SerialPort class in a For Each...Next loop. In the loop, the wmiPort variable holds the data for each serial port. The DeviceID property, in turn, retrieves each serial port's device ID. The code at callout A in Listing 1 adds the retrieved device ID and a couple of tabs to the str variable. (The tabs keep the data formatted in columns.)

To retrieve the IRQs and I/O ports, I needed to query the machine. Typically, you use the WMI Query Language (WQL) to query machines for WMI data, so I used WQL's Associators Of statement to assemble the query at callout B in Listing 1. The Associators Of statement retrieves linked data. In this case, I'm retrieving a set of data associated with a certain serial port (wmiPort.DeviceID) that relates to that port's allocated resources (Win32_ AllocatedResource). Because serial ports have IRQs and I/O ports assigned to them, this WQL query retrieves the IRQs and I/O ports for each serial port.

To execute the query, I applied the ExecQuery method, passing the set of results to wmiAssociatorSet. I then used a For Each...Next loop to go through the set of results, pulling each individual result into wmiAssociator and applying the Name property to retrieve the associated data. Finally, I added the data to my str variable with suitable formatting and used the Echo method to display the resulting string.

You can download QueryPorts.wsf from the Code Library on the Windows Scripting Solutions Web site ( You can run this script on Windows 2000, Windows 98, and Windows Millennium Edition (Windows Me). To use this script, you need to install the Windows Script (WS) 5.1 or later and WMI components. (For more information about installing the WS 5.1 and WMI components, see "Scripting Solutions with WSH and COM: A New Column About Task Automation," June 2000," and "Scripting Solutions with WSH and COM: Simple Uses of WMI," July 2000, respectively.) To run QueryPorts.wsf, type

wscript QueryPorts.wsf

at the command line or double-click the icon in Windows Explorer. Figure 1 shows an example of the results you'll obtain.

Next Month
As I have just demonstrated, WMI scripts can be easy to write. However, the ease with which you can write them doesn't mean they aren't useful. QueryPorts.wsf illustrates just how effective WMI scripts can be in retrieving relevant machine information.

Next month, I'll continue my series on how to use WSC to create a progress bar dialog box. I'll show you how to complete the script component and put it to good use.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.