WMI provides a variety of techniques to retrieve and manipulate WMI class and instance information, using Microsoft Visual Basic Scripting Edition (VBScript) and C++.
The following table lists the topics that discuss the techniques to retrieve and manipulate WMI class and instance information.
Manipulating Data Using VBScript
You can use direct access to access the WMI properties of a WMI class or instance directly on an
SWbemObject, rather than through the property
collection of that object. You can also execute methods on that object in the native style of the programming language rather than using the
SWbemServices.ExecMethod call.
SWbemServices.ExecMethod is primarily useful in calling provider methods in JScript or when a script must run on different operating systems. For example, the
Create method in
Win32_Process had three parameters in Windows 2000 but has four parameters in Windows XP.
Using direct access, you can treat WMI properties and methods as if they were automation properties and methods of
SWbemObject.
The following example shows how you can access a property.
VolumeName = MyDisk.Properties_("VolumeName")
The following example shows how you can access a property when you have direct access.
VolumeName = MyDisk.VolumeName
Chaining of objects is also acceptable.
The following example shows how to access a property of an object that is embedded in another object.
value = MyComputer.MyDisk.VolumeName
The following example shows how to access a property with array subscript notation.
valueOfElement = MyDisk.MyArrayProperty(3)
The following VBScript code example shows how to spawn an instance of the input parameters to the
Create method in the
Win32_Process class as an
SWbemObject, populate the input properties, and then execute the
Create method using
SWbemServices.ExecMethod.
The
SWbemObject.Methods_ property returns an
SWbemMethodSet collection of the
Win32_Process methods. The members of the method set are
SWbemMethod objects and
SWbemMethod.InParameters returns the input parameters for the
Create method. The required CommandLine input parameter is set to "calc.exe". The method is then executed by
SWbemServices.ExecMethod, resulting in the launch of a calc.exe process.
set services = GetObject("winmgmts:root\cimv2")
Set obj = Services.Get("Win32_Process")
Set objIns = obj.Methods_("Create").InParameters.SpawnInstance_
objIns.CommandLine = "calc.exe"
Set objOut = Services.ExecMethod("Win32_Process", "Create", objIns)
MsgBox "Return value = " & objOut.returnvalue & _
VBCRLF & "Process ID = " & objout.processid
The following code example shows how to perform the previous operation using direct access.
set services = GetObject("winmgmts:root\cimv2")
Set obj = Services.Get("Win32_Process")
returnvalue = obj.create("calc.exe",,,processid)
MsgBox "Return value = " & returnvalue & _
VBCRLF & "Process ID = " & processid
For more information, see
Calling a Provider Method and
Scripting with SWbemObject.
Send comments about this topic to Microsoft
Build date: 11/3/2009