Using the WMI Scripting Type Library
You can use the WMI scripting type library to call WMI Scripting API methods from Microsoft Visual Studio and in Windows Script Host WSF files.
Note Visual InterDev 6.0 features have been integrated into Microsoft Visual Studio .NET.
The following procedure describes how to enable the integrated development environment (IDE) to be aware of the WbemScripting type library.
To add the WMI Scripting type library to the project references
- Select Add References from the Project menu.
- In the COM tab of the Add Reference box, select Microsoft WMI Scripting V1.2 Library.
- If no suitable option appears in the References list, add it by using Browse in the
References box. The Browse opens an Add Reference box that enables you to locate the WbemScripting type library.
The WbemScripting type library resides in the file Wbemdisp.tlb in the %windir%\System32\Wbem directory.
- Select the file and click Open. Microsoft WMI Scripting V1.2 Library appears on the references list. Ensure that you select the box next to this item in the list.
You can include the reference to the WbemScripting.SWbemLocator in a Windows Script Host WSF file, unlike a script written in Visual Basic, Scripting Edition or other scripting languages. This enables you to use constant names instead of values. For example, use WbemAuthenticationLevelPktPrivacy rather than the value 6 when setting authentication.
Scripts can connect with the Scripting API for WMI type library using the following methods:
- Specifying the WbemScripting GUID in the VBScript methods CreateObject and
This alerts Windows Script Host to connect to the WMI object set.
The following VBScript code example creates a new SWbemDateTime object.
- Using the Moniker string "winmgmts:" when obtaining a new or existing object.
The following VBScript code example uses the "winmgmts:" moniker to get the instance of Win32_Process with a Handle property of 0 (zero). Handle is the key property for this class.
- Referencing the WMI type library using the <reference> tag of the WSH 2.0 XML file format. If you use the <reference> tag, the tag must have either a uuid attribute whose value is the GUID of the WMI type library, or (recommended) an object attribute whose value is the PROGID of any of the WMI scripting objects you can create.
The following VBScript code example uses the PROGID of "WbemScripting" . To run the script, save the text in a file with a .wsf extension.
<?xml version="1.0" encoding="US-ASCII"?> <job> <reference object="WbemScripting.SWbemLocator"/> <script language="VBScript"> set service = GetObject("winmgmts:") ' Following line uses a symbolic ' constant from the WMI type library service.Security_.impersonationLevel = _ wbemImpersonationLevelDelegate </script> </job>
- Using an <object> tag to create a WMI scripting object. You can specify the id attribute with the value of a name that references the WMI scripting object you want to create, and the progid attribute equal to the PROID of the WMI scripting object.
The following WSH script displays the hostname and the number of processors on the local computer. To run the script, save the text in a file with a .wsf extension.
<?xml version="1.0" encoding="US-ASCII"?> <job> <object id="objSWbemLocator" progid="WbemScripting.SWbemLocator"/> <script language="VBScript"> strComputer = "." Set objSWbemServices = objSWbemLocator.ConnectServer _ (strComputer, "root\cimv2") Set colSettings = objSWbemServices.ExecQuery( _ "Select * From Win32_ComputerSystem") For Each objComputer in colSettings Wscript.Echo "System Name: " & objComputer.Name Wscript.Echo "Number of Processors: " & _ objComputer.NumberOfProcessors Next </script> </job>
Build date: 11/19/2012