Scripting in WMI
You can view or manipulate any information made available through WMI using scripts that access the Scripting API for WMI. Scripts can be written in any scripting language that supports Microsoft ActiveX script hosting, including Visual Basic Scripting Edition (VBScript), PowerShell, and Perl. Windows Script Host (WSH), Active Server Pages, and Internet Explorer can all host WMI scripts.
The following VBScript code example illustrates a number of WMI scripting features. This script enumerates all of the disks on the local computer system.
On Error Resume Next For Each Disk In GetObject( _ "winmgmts:").InstancesOf ("CIM_LogicalDisk") WScript.Echo "Instance:", Disk.Path_.Relpath Next If Err <> 0 Then set lasterr = CreateObject("WbemScripting.SWbemLastError") Wscript.echo lasterr.Operation End If
If this script is saved in a text file and run, it is executed by WSH. A window is displayed for each disk object on the local computer (for example, "Instance: Win32_LogicalDisk.DeviceID="A:"").
The following example is a compact line of code that accomplishes several tasks.
In the previous code example, the following tasks are accomplished:
- Uses the moniker "winmgmts:" to tell WSH to use
Scripting API objects.
For more information about monikers, see Creating a WMI Script.
- Locates the WMI service on the local computer.
- Connects to the default namespace called "root\cimv2".
A namespace is the highest level object in the WMI data, essentially the beginning of the path to a WMI object. This connection returns an SWbemServices object. For more information, see Describing the Location of a WMI Object.
- Uses the default security level that is assumed by the "winmgmts:" moniker.
For more information, see Setting the Default Process Security Level Using VBScript.
- Calls the
SWbemServices.InstancesOf method to retrieve all WMI Windows logical disk objects into a
SWbemObjectSet container object, which is treated in script as a collection that can be enumerated.
For more information, see Accessing a Collection.
- Sets up a loop that iterates through the objects in the SWbemObjectSet collection.
- Illustrates the heavy use of class inheritance in WMI by finding instances of the CIM_LogicalDisk class.
For more information, see Common Information Model. The CIM_LogicalDisk CIM class is the parent of the Win32_LogicalDisk class. This is a preinstalled WMI class that represents logical disks on a Windows-based computer system. WMI supplies an entire schema of such preinstalled classes that allow you to access and control managed objects. For more information, see Win32 Classes.
- Accesses WMI semisynchronously that is, the call returns immediately and any waiting for access occurs during the loop through the collection.
WMI also provides asynchronous calls in which an event is used to notify the script that objects are available or that the call is completed. For more information, see Calling a Method.
No instances of CIM_LogicalDisk exist because it is an abstract base class. However, SWbemServices.InstancesOf and comparable WMI queries locate all of the child classes that inherit from CIM_LogicalDisk. For more information, see WMI Classes.
The following example illustrates several important features of WMI scripting.
The following features are illustrated in the previous code example:
Path_ is a property of the special generic object
Properties or methods ending in an underscore usually belong to this extended object, SWbemObjectEx. In the script line above, The Disk variable refers to a Win32_LogicalDisk instance, which does not have a Path method (or equivalent). Because any WMI object can use the methods of SWbemObject, Disk.Path_ can be called to return an SWbemObjectPath object containing the WMI path for the disk object. The generic object, SWbemObject, can represent any WMI object.
WMI paths can locate a particular object in a namespace.
The SWbemObjectPath.Relpath property is displayed by WSH; for example, ""Win32LogicalDisk.DeviceID="D""".
- Properties with the Key qualifier are the final part of the path to locate a particular instance.
- Direct access and chaining objects, methods, and properties.
You can directly access the properties and methods of WMI objects as shown in Disk.Path_.Relpath. For more information, see Manipulating Class and Instance Information.
Note When querying for property values with a uint64 or sint64 data type in a scripting language like VBScript, WMI returns string values. Unexpected results can occur when comparing these values, because comparing strings returns different results than comparing numbers. For example, "10000000000" is less than "9" when comparing strings, and 9 is less than 10000000000 when comparing numbers. To avoid confusion you should use the CDbl method in VBScript when properties of type uint64 or sint64 are retrieved from WMI.
The following code example demonstrates that error handling in WMI scripts goes beyond the capabilities of the WSH Err object. By creating an SWbemLastError object, you can get WMI-specific information on the error that occurred. Note that the SWbemLastError object is created by the standard VBScript CreateObject method with the ProgID value of "WbemScripting.SWbemLastError". This is another method of obtaining a WMI object.
If Err <> 0 Then set lasterr = CreateObject("WbemScripting.SWbemLastError") Wscript.Echo lasterr.Operation End If
For more information about books, websites, and articles on writing WMI scripts and using WMI, see Further Information.
Build date: 11/19/2012