Writing WMI Scripts

One point that has been reiterated throughout this chapter is that most WMI scripts follow a simple three-step pattern. In general, WMI scripts:

Connect to the WMI service.

Retrieve a WMI object or collection of objects.

Perform some sort of task on the object or objects. Until now, this task has largely been confined to reporting property values. However, you can also do such things as run methods, configure property values, and create or delete instances.

To become proficient in writing WMI scripts, you need to understand the nuances of these three steps. The preceding section of this chapter was a bit more theoretical in nature: It provided useful background information for understanding what WMI is and what it can do. This portion of the chapter takes a more practical approach; in particular, it provides a detailed look at the three steps used in most WMI scripts. This is done by:

Explaining the use of the WMI moniker, a versatile method for connecting to the WMI service.

Explaining the use of ExecQuery, an SWbemServices method that provides an alternative (and typically better and faster) way to retrieve WMI data.

Providing a series of templates that can serve as the basis for scripts that carry out common tasks such as retrieving information, configuring property values, executing methods, and creating or deleting instances.

In addition to detailing the three steps in a typical WMI script, this section also looks at two other important elements in script writing: working with WMI dates and times, and monitoring events.