Previous Section Table of Contents Next Section

Testing the Query

In the previous chapter, I showed you how to write and test a query using the Wbemtest tool. I recommend that you test every query you plan to write, by running Wbemtest on the target operating system. That way, you'll know your queries are returning the correct results before you spend a lot of time writing an actual script.

For specific instructions on testing a query, see "Really-It's This Easy" in Chapter 17.

If your script will run on multiple operating systems (as in a logon script or a script being used to manage multiple remote servers), be sure to test the query on each potential operating system. That way, you'll quickly spot any WMI version incompatibilities, and you can take the appropriate steps. Don't forget that you can also test your query by using a generic WMI query script, such as the kind generated by the WMI Scriptomatic or by PrimalScript's WMI Query Wizard.

For example, suppose I want to test the Win32_QuotaSetting query. By using PrimalScript, I just run the wizard, select Win32_QuotaSetting from the class list, and click Insert. The wizard creates the following script.


On Error Resume Next

Dim strComputer

Dim objWMIService

Dim colItems



strComputer = "."

Set objWMIService = GetObject( _

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

Set colItems = objWMIService.ExecQuery( _

 "Select * from Win32_QuotaSetting",,48)

For Each objItem in colItems

 WScript.Echo "Caption: " & objItem.Caption

 WScript.Echo "DefaultLimit: " & objItem.DefaultLimit

 WScript.Echo "DefaultWarningLimit: " & _

  objItem.DefaultWarningLimit

 WScript.Echo "Description: " & objItem.Description

 WScript.Echo "ExceededNotification: " & _

  objItem.ExceededNotification

 WScript.Echo "SettingID: " & objItem.SettingID

 WScript.Echo "State: " & objItem.State

 WScript.Echo "VolumePath: " & objItem.VolumePath

 WScript.Echo "WarningExceededNotification: " &

  objItem.WarningExceededNotification

Next

If I want to make a more complex query, I can just modify the template before testing the script. For example, I might change the query to something like this.


Set colItems = objWMIService.ExecQuery( _

 "Select * from Win32_QuotaSetting WHERE " & _

 "VolumePath = "C:\\",,48)

This revised query would return all quota settings affecting the C: volume, as opposed to all quota settings on the entire server. Then, I can save the query, copy it to whatever servers I plan to run the final script on, and run the query. The template scripts generated by Scriptomatic and the Query Wizard are noninvasive, meaning they only display information rather than try to change it. That makes them perfect for generating harmless test scripts that allow you to make sure your queries run without error.

Perhaps one of the most annoying aspects of troubleshooting WMI queries is that they don't often return error messages. Consider this example.


On Error Resume Next

Dim strComputer

Dim objWMIService

Dim colItems



strComputer = "."

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

 strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery( _

 "Select * from Win32_Service WHERE Nmae = 'spooler'",,48)

For Each objItem in colItems

 WScript.Echo "Caption: " & objItem.Caption

Next

This example began life as a PrimalScript WMI Query Wizard-generated template, but I modified the WQL query. If you look closely, you'll see that I did it wrong: Name is spelled Nmae. Nonetheless, running this script as-is produces no error of any kind, from either VBScript or WMI. That's because WMI looks for all instances of Win32_Service that have a Nmae property set to "spooler." It doesn't find any, of course, because no instance of Win32_Service has a Nmae property. So, the script completes cleanly without returning any information.

If your queries aren't returning instances, and you think they should, double- and triple-check the spelling of your class names and service names.

    Previous Section Table of Contents Next Section