Scripting with SWbemObject

The SWbemObject scripting object is the generic WMI object, defining properties and methods that can be used regardless of the specific WMI object to which the SWbemObject object is bound. All WMI objects, such as an instance of Win32_Process or any other WMI data class, are represented by SWbemObject and can use the SWbemObject common properties and methods in addition to their own particular properties and methods.

For example, use the following script to obtain all of the instances of a Win32_Process by calling the SWbemObject.Instances_ method. The clsobjProcess represents both the Win32_Process class definition and an SWbemObject.

strComputer = "."
Set objWMIServices = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set clsobjProcess = objWMIServices.Get("Win32_Process")
Set colProcesses = clsobjProcess.Instances_()
For Each Process in colProcesses
    WScript.Echo Process.Name
Next

The following example obtains a specific instance of Win32_Service that represents the Alerter service and stores it in objAlerter. You can then call either SWbemObject methods, such as WScript.Echo objAlerter.Path_, or methods defined by the data class, such as WScript.Echo objAlerter.State. objAlerter which represents both an instance of Win32_Service and an SWbemObject.

strComputer = "." 
Set objWMIServices = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set objAlerter = objWMIServices.Get("Win32_Service.Name='Alerter'")
WScript.Echo objAlerter.Path_
objAlerter.StopService()
WScript.Echo objAlerter.State
For each Prop in myObject.Properties_
    Wscript.Echo Prop.Name
Next

The call to SWbemObject.Instances_ obtains another generic WMI scripting object, SWbemObjectSet. As shown, the SWbemObjectSet object can be treated as a collection.

Set clsobjProcess = objWMIServices.Get("Win32_Process")

You can identify the SWbemObject methods because they all end with an underscore (_), for example, SWbemObject.Instances_.

SWbemObjectEx extends the properties of SWbemObject. For example, you can now update the data of any WMI object, such as an instance of Win32_Process, by a call to SWbemObjectEx.Refresh_.

The following example shows how the system process page fault data can be refreshed every five seconds.

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'System'",,48) 
For Each Process in colProcesses
        i = 0
        Do Until i = 5
            i = i + 1
            Wscript.Echo "PageFaults = " & Process.PageFaults 
            Wscript.Sleep 10000
            Process.Refresh_
        Loop
Next

For more information about refreshing data using an SWbemRefresher object, see Refreshing WMI Data in Scripts.

The SWbemObject.Put_ and PutAsync_ allow you to write changes back to any WMI object. These methods only commit changes to an object in the namespace where the object was created. You can write the object to a different namespace using SWbemServicesEx.Put or SWbemServicesEx.PutAsync.

Scripting API for WMI

Creating a WMI Script

Updating an Entire Instance

Calling a Method