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 an 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.
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 strComputer = "."
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
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
Windows 2000: SWbemServicesEx is not available.
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.
Build date: 11/19/2012