Previous Page
Next Page

Tell Me Everything About Everything!

When novices first write Windows Management Instrumentation (WMI) scripts, they nearly all begin by asking for every property about all instances of a class that are present on a particular system. (This is also referred to as the infamous "Select * query".) This approach can often return an overwhelming amount of data, particularly when you are querying a class such as installed software, or processes and threads. Rarely would one need to have so much data. Typically, when looking for installed software, you're looking for information about a particular software package.

There are, however, several occasions when I want to use the "tell me everything about all instances of a particular class" query:

  • During development of a script to see representative data

  • When troubleshooting a more directed query (for example, when I'm possibly trying to filter on a field that does not exist)

  • When the returned data is so small that being more precise doesn't make sense

Just the Steps

To return all information from all instances

1.
Make a connection to WMI.

2.
Use the Select statement to choose everything: Select *.

3.
Use the From statement to indicate the class from which you wish to retrieve data. For example, From Win32_Share.


In the next script, you make a connection to the default namespace in WMI and return all the information about all the shares on a local machine. This is actually good practice, because in the past, numerous worms propagated via unsecured shares, and you might have unused shares arounda user might create a share for a friend and then forget to delete it. In the script that follows, called ListShares.vbs, all the information about shares present on the machine is reported.

ListShares.vbs

Option Explicit
On Error Resume Next
Dim strComputer
Dim wmiNS
Dim wmiQuery
Dim objWMIService
Dim colItems
Dim objItem

strComputer = "."
wmiNS = "\root\cimv2"
wmiQuery = "Select * from Win32_Share"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

For Each objItem In colItems
  WScript.Echo "AccessMask: " & objItem.AccessMask
  WScript.Echo "AllowMaximum: " & objItem.AllowMaximum
  WScript.Echo "Caption: " & objItem.Caption
  WScript.Echo "Description: " & objItem.Description
  WScript.Echo "InstallDate: " & objItem.InstallDate
  WScript.Echo "MaximumAllowed: " & objItem.MaximumAllowed
  WScript.Echo "Name: " & objItem.Name
  WScript.Echo "Path: " & objItem.Path
  WScript.Echo "Status: " & objItem.Status
  WScript.Echo "Type: " & objItem.Type
  WScript.Echo
Next

Header Information

The Header information section of ListShares.vbs contains all the standard information. Option Explicit forces the declaration of all variables. This is followed by On Error Resume Next to make sure the script goes to the next line of code if it encounters an error.

Note

In Chapter 1, "Starting from Scratch," we talked about the pros and cons of using On Error Resume Next. Most of the time, when you are working with WMI, you are displaying property values, which is a harmless activity. Using On Error Resume Next helps the script to run, even when the script encounters an error. This is largely a good thing with WMI.


These two standard lines are followed by the same variable names declared in previous WMI scripts: strComputer, wmiNS, wmiQuery, objWMIService, colItems, and objItem. The variable strComputer defines the target computer, wmiNS specifies the target WMI namespace, wmiQuery holds the value of the query to be executed, and colItems holds the collection of items that are returned by the query. The variable objItem is used by the For Each...Next loop to iterate through the collection.

Reference Information

The Reference information section of the script is used to assign values to five of the six variables. The variable strComputer is assigned the value of ".", which indicates the script will run against the local computer. The variable wmiNS is assigned to \root\cimv2, which is the default WMI namespace. The variable wmiQuery is set to "Select * from Win32_Share". This is the query you want to execute against the default WMI namespace. Select * tells WMI that you want to retrieve all properties from the Win32_Share object. Note that this query doesn't display all the properties; it simply displays all the properties from the Win32_Share object. What you do with the returned data depends on your current needs. Unless you need it, returning all the data might not be a very efficient use of networking resources. It is, however, very easy to construct such a query.

The variable objWMIService is used to connect to WMI, and it uses the WMI moniker to do so. Two variables assist in this operation: strComputer and wmiNS. The colItems variable holds the handle that comes back from the ExecQuery method that is used to execute your WMI query against the Win32_Share class.

Worker and Output Information

The Worker information and Output information sections of the ListShare.vbs script are combined, and the script simply uses WScript.Echo to write the various properties and their associated values to the command line (if run in CScript) or to a pop-up dialog box (if run in WScript, which is not a really good idea when you have numerous shares). The most convenient listing of all the available properties for a particular class is contained in the Platform SDK. A quick search for Win32_Share reveals the properties listed in Table 10-1.

Table 10-1. Win32_Share Properties

Data type

Property

Meaning

Boolean

AllowMaximum

Allow maximum number of connections? True or False.

string

Caption

Short, one-line description.

string

Description

Description.

datetime

InstallDate

When the share was created (optional).

uint32

MaximumAllowed

Number of concurrent connections allowed. Only valid when AllowMaximum is set to False.

string

Name

Share name.

string

Path

Physical path to the share.

string

Status

Current status of the share: Degraded, OK, or Failed.

uint32

Type

Type of resource shared: disk, file, printer, and so on.


Quick Check

Q.

What is the syntax for a query that returns all properties of a given object?

A.

Select * returns all properties of a given object.

Q.

What is one reason for using Select * instead of a more directed query?

A.

In troubleshooting, Select * is useful because it returns any available data. In addition, Select * is useful in trying to characterize the data that might be returned from a query.



Previous Page
Next Page