The "WinMgmts:" PrefixMicrosoft® Windows® 2000 Scripting Guide WMI monikers can consist of three parts: one mandatory component and two optional components. The mandatory component is the "winmgmts:" prefix. All WMI monikers must begin with "winmgmts:" as shown here: Set objSWbemServices = GetObject("winmgmts:")
The moniker in this code is the string "winmgmts:", which is passed to the GetObject function. Although in this example the string is entered using all lowercase letters, you can use whatever case you like; that is, "WinMgmts:", "WINMGMTS:", and "winmgmts:" all produce the same result. Specifying a moniker that consists only of the "winmgmts:" prefix is the most basic form of WMI moniker you can use. The result is always a reference to an SWbemServices object, which represents a connection to the Windows Management Instrumentation service on the local computer. Under the covers, the "winmgmts:" moniker: 1. | Retrieves the WMI CLSID from the registry subkey HKCR\WINMGMTS\CLSID. The CLSID ({172BDDF8-CEEA-11D1-8B05-00600806D9B6}) is the identifier used by the operating system to map WMI to the appropriate COM object. | 2. | Retrieves the value from a second registry entry, HKCR\CLSID\{172BDDF8-CEEA-11D1-8B05-00600806D9B6}\InProcServer32. This value (typically C:\Windows\System32\wbem\wbemdisp.dll) indicates the path to the COM object that exposes the SWbemServices object. | 3. | Loads Wbemdisp.dll, the DLL containing the WMI scripting library that exposes SWbemServices. |
After you have obtained a reference to SWbemServices, you can then invoke one of the object methods as shown here: Set objSWbemServices = GetObject("winmgmts:") Set colSWbemObjectSet = objSWbemServices.InstancesOf("Win32_LogicalDisk")
In this example, a reference variable named objSWbemServices is initialized using the "winmgmts:" moniker. This reference variable is subsequently used to invoke the InstancesOf method provided by the SWbemServices object. Although the preceding example is perfectly acceptable, you do not have to use two lines of code to retrieve all Win32_LogicalDisk instances. This can also be done with the following single line of script: Set colSWbemObjectSet = GetObject("winmgmts:").InstancesOf("Win32_LogicalDisk")
In this case, a user-defined variable (objSWbemServices in the example preceding this one) is not used to explicitly reference the SWbemServices object commonly returned by GetObject and the WMI moniker. Instead, the "winmgmts:" moniker creates an SWbemServices reference in memory and immediately uses the unnamed, memory based reference to call the SWbemServices InstancesOf method. In the end, both examples produce identical results in the form of an SWbemObjectSet collection containing all instances of the Win32_LogicalDisk class on the local computer. You can also call the ExecQuery method or any other method provided by the SWbemServices object. In fact, if the objective of your script is to simply enumerate and echo all Win32_LogicalDisk instances, you can get by with as little as the following: For Each objDisk In GetObject("winmgmts:").InstancesOf("Win32_LogicalDisk") Wscript.Echo objDisk.DeviceID Next
In this case, user-defined variables are not used to reference SWbemServices or SWbemObjectSet. Both objects are still created, but only in memory and without an explicit object reference. By using the most basic WMI moniker, and by understanding the relationship of the moniker with the WMI scripting library, you can begin to construct concise yet powerful WMI statements. 先剪下来,等会再看 ,谢谢啊
[此贴子已经被作者于2006-11-20 10:09:24编辑过] |