Previous Page
Next Page

Querying WMI

In most situations, when you use WMI, you are performing some sort of query. Even when you're going to set a particular property, you still need to execute a query to return a dataset that enables you to perform the modification to the property. (A dataset is the data that comes back to you as the result of a query, that is, it is a set of data.) In this section, you'll look at the methods used to query WMI.

Just the Steps

To query WMI

1.
Specify the computer name.

2.
Define the namespace.

3.
Connect to the provider using GetObject.

4.
Issue the query.

5.
Use For Each...Next to iterate through collection data.


One of the problems with Windows Server 2003 for the small to medium enterprise is Windows Server 2003 product activation. Although the larger customers have the advantage of "select" keys that automatically activate the product, smaller companies often are not aware of the advantages of volume licensing and as a result do not have access to these keys. In addition, I've seen larger customers use the wrong keyyou can easily forget to activate the copy of Windows Server 2003. Many customers like to monitor the newly built machine prior to actual activation because of the problems resulting from multiple activation requests. As is often the case with many information technology (IT) departments, emergencies arise, and it is easy to forget to make the trek back to the server rooms to activate the machines. This is where the power of WMI scripting can come to the rescue. The following script, Display WPAStatus.vbs, uses the new Win32_WindowsProductActivation WMI class to determine the status of product activation.

DisplayWPAStatus.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_WindowsProductActivation"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

For Each objItem In colItems
    WScript.Echo "ActivationRequired: " & objItem.ActivationRequired
    WScript.Echo "IsNotificationOn: " & objItem.IsNotificationOn
    WScript.Echo "ProductID: " & objItem.ProductID
    WScript.Echo "RemainingEvaluationPeriod: " & _
        objItem.RemainingEvaluationPeriod
    WScript.Echo "RemainingGracePeriod: " & objItem.RemainingGracePeriod
    WScript.Echo "ServerName: " & objItem.ServerName
Next

Header Information

The Header information section of DisplayWPAStatus.vbs contains the two normal items, Option Explicit and On Error Resume Next. (If you are unfamiliar with these commands, refer to Chapter 1, "Starting from Scratch.") Next, you declare six variables to be used in this script. Because you are writing a WMI script, you make up some new variable names. Table 8-1 lists the variables and their intended use in this script.

Table 8-1. Variables used in DisplayWPAStatus.vbs

Variable name

Variable use

strComputer

Holds the name of the computer the query will target at run time

wmiNS

Holds the namespace that the WMI query will target

wmiQuery

Holds the WMI query

objWMIService

Holds the connection to the WMI service

colItems

Holds the collection of items returned by the WMI query

objItem

Holds the individual item from which the properties will be queried


Reference Information

The Reference information section of the script is used to assign value to some of the variables declared in the Header information section. The first variable used in the Reference information section is strComputer, whose value is set to ".". In WMI shorthand, "." is used to mean "this computer only." So the WMI query will operate on localhost. The second variable assigned a value is wmiNS, which is used to hold the value of the WMI namespace you query. You could include the namespace and the query on the same line of the script; however, by breaking the namespace and the query out of the connection string, you make it easier to reuse the script. The next variable is wmiQuery, which receives the value of "Select * from Win32_WindowsProductActivation". You can easily change the query to ask for other information. You are asking for everything that is contained in the local computer from the Win32_WindowsProductActivation namespace.

You use the Set command to set objWMIService to the handle that is obtained by the GetObject command. The syntax for this command is very important because it is seminal to working with WMI. When making a connection using winmgmts://, winmgmts is called a moniker. A moniker works in the same way that the phrase "abracadabra" used to work in the old movies. It's a shortcut that performs a lot of connection work in the background. Remember the magic phrase winmgmts because it will do much of the work for you, including opening the door to the storehouse of valuable WMI data. The last item in the Reference information section is the use of the variable colItems, which is used to object returned by the ExecQuery method of the SWbemServices object. The Reference information section follows:

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

Worker and Output Information

The Worker information section is the part of the script that works through the collection of data returned and produces the Windows Product Activation (WPA) information. This section is always going to be customized for each WMI script you write, because each query or each provider used returns customized data.

Because WMI returns data in the form of a collection, you need to use a For Each...Next loop to iterate through the items in the collection. This loop is requiredeven when WMI returns only one item, WMI still returns that item in a collection. Your question at this point is probably "How do I know what to request from WMI?" I looked that up in the Platform SDK. By looking in the SDK for Win32_WindowsProductActivation, I learned that several properties of interest to a network administrator will return information. The SDK also told me that the properties are all read-only (which would prevent us from flipping the ActivationRequired field to false. The Worker and Output information section of this script follows:

For Each objItem In colItems
    WScript.Echo "ActivationRequired: " & objItem.ActivationRequired
    WScript.Echo "IsNotificationOn: " & objItem.IsNotificationOn
    WScript.Echo "ProductID: " & objItem.ProductID
    WScript.Echo "RemainingEvaluationPeriod: " & _
        objItem.RemainingEvaluationPeriod
    WScript.Echo "RemainingGracePeriod: " & objItem.RemainingGracePeriod
    WScript.Echo "ServerName: " & objItem.ServerName
Next

The most interesting information in Win32_WindowsProductActivation is listed in Table 8-2.

Table 8-2. Properties of Win32_WindowsProductActivation

Property

Meaning

ActivationRequired

If 0, activation is not required. If 1, the system must be activated within the number of days indicated by the RemainingGrace Period property.

IsNotificationOn

If 0, notification reminders and the activation icon are disabled. If not equal to 0 and product activation is required, notification reminders (message balloons) are enabled, and the activation icon appears in the notification tray.

ProductID

A string of 20 characters separated by hyphens. This is the same product ID that is displayed on the General tab of the System Properties dialog box in Control Panel.

RemainingEvaluationPeriod

If beta or evaluation media, this returns the number of days remaining before expiration. If retail media, this field is set to the largest possible unsigned value.

RemainingGracePeriod

Number of days remaining before activation is required if ActivationRequired is equal to 1.

ServerName

Name of the system being queried. This could also be the IP address of the system.



Previous Page
Next Page