Referencing an Object

To reference objects in WMI, you use a UNC-style path name. Here is an example of how to reference the C: drive on the host terminator:

\\terminator\root\CIMv2:Win32_LogicalDisk.DeviceID="C:"

The first part of the path (\\terminator\) is a reference to the computer on which the object resides. To reference the computer on which the script is running, you can use a dot (.) for the computer name. The second part (root\CIMv2) is the namespace the object resides in. Each WMI provider uses a namespace to store its associated objects. The third part (Win32_LogicalDisk) is the class of the object to reference. The fourth part is the key/value pairs representing an object of that class. Generically, the path can be described as follows:

\\ComputerName\NameSpace:ClassName.KeyName="KeyValue"[,KeyName2="KeyValue2" . . . ]

Now that we know how to reference WMI objects, let’s instantiate an object using VBScript’s GetObject function. In order for GetObject to understand we are referencing WMI objects, we have to include one additional piece of information: the moniker. If you’ve done any Active Directory scripting before, you’re probably familiar with the LDAP: and WinNT: monikers used in ADSI. For WMI, we need to use the winmgmts: moniker:

set objDisk = GetObject("winmgmts:\\terminator" & _
                        "\root\CIMv2:" & _
                        "Win32_LogicalDisk.DeviceID='C:'")

To accomplish the same thing in Perl, we need to use the Win32::OLE module. (The sidebar details differences between VBScript and Perl.) ...

Get DNS on Windows Server 2003, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.