Creating a WMI Script
WMI scripts can condense many of the steps required in a C++ program. They can connect to WMI, not only through an SWbemLocator object, but also through the moniker "winmgmts:". A moniker is a short name that locates a namespace, class, or instance in WMI. The name "winmgmts:" is the WMI moniker that tells the Windows Script Host to use the WMI objects, connects to the default namespace, and obtains an SWbemServices object. Other connection information, such as an impersonation level or a specific class or instance, appears in the string following the moniker name. You can use monikers in calls that either create or get WMI objects. For more information, see Constructing a Moniker String.
The following procedure describes how to connect to WMI using SWbemLocator.
To connect to WMI using SWbemLocator
- Retrieve a locator object with a call to CreateObject.
Set Locator = CreateObject("WbemScripting.SWbemLocator")
- Log on to the namespace using a call to the
ConnectServer method.
Set objLocator = CreateObject("WbemScripting.SWbemLocator") Set objService = objLocator.ConnectServer(".", "root\cimv2")
If you do not specify a computer in the call to ConnectServer, then WMI connects to the local computer. If you do not specify a namespace, then WMI connects to the namespace specified in the registry key.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WBEM\Scripting\Default Namespace
The default namespace is \root\cimv2. For more information about namespaces, see Creating Hierarchies Within WMI.
- Set the impersonation level with a call to the SWbemServices.Security_ method.
objService.Security_.ImpersonationLevel = 3
For more information, see Setting the Default Process Security Level Using VBScript.
- Implement the purpose of your script.
WMI exposes a variety of scripting objects that use to access and manipulate data across your network. For more information, see Manipulating Class and Instance Information and Scripting API for WMI.
Set objLocator = CreateObject("WbemScripting.SWbemLocator") Set objService = objLocator.ConnectServer(".", "root\cimv2") objService.Security_.ImpersonationLevel = 3 Set Jobs = _ objService.ExecQuery("SELECT * FROM Win32_ScheduledJob") i=0 For each Job in Jobs i = i+1 WScript.Echo Job.JobId & " " _ & Job.Command & VBNewLine Next If i = 0 Then WScript.Echo "No Jobs Scheduled with the AT command were found" End If
The following procedure describes how to connect to WMI and retrieve an object using a moniker.
To connect to WMI and retrieve an object using a moniker
- Call
GetObject with a moniker in the input parameter.
Set MyObject = GetObject( _ "winMgmts:{impersonationLevel=impersonate}" _ & "!Win32_Service=""ALERTER""")
For more information about using the VBScript method GetObject in WMI scripts, see Creating an Instance and Retrieving an Instance.
In the previous example, impersonate, or impersonationLevel=3, is the default process security level. In the following example, it is not necessary to specify this process security level unless you need to change the process security to delegate. For more information, see Setting the Default Process Security Level Using VBScript.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colScheduledJobs = objWMIService.ExecQuery _ ("Select * from Win32_ScheduledJob") For Each objJob in colScheduledJobs Wscript.Echo "Job ID: " & objJob.JobId & _ "Command: " & objJob.Command & VBNewLine Next
Related topics
Send comments about this topic to Microsoft
Build date: 11/19/2012