Bewerken

Delen via


Manipulating Class and Instance Information

WMI provides a variety of techniques to retrieve and manipulate WMI class and instance information, using Microsoft PowerShell, 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.

Topic Description
Retrieving WMI Class or Instance Data Retrieve and set data from and to the WMI information repository.
Modifying an Instance Property Change the information in the instance after it is retrieved.
Changing the Inheritance of an Instance Change the parent class of an instance.
Modifying a Method Modify the parameters of an instance.
Enumerating WMI Enumerate WMI objects.
Querying WMI Query WMI objects.
Calling a Method Use associated methods created by Microsoft or other third-party developers to further manipulate WMI objects, or else directly affect the object that the WMI object represents.
Accessing a Collection Enumerate collections in script.

 

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. For example, the Create method in Win32_Process had three parameters in Windows 2000 but has four parameters in Windows Server 2003.

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.