Refreshing WMI Data in Scripts

In monitoring scripts, you can avoid successive calls to GetObject by using an SWbemRefresher object. The SWbemRefresher object is a container that can hold several WMI objects whose data can be refreshed in one call.

Using an SWbemRefresher object is required to get accurate data from WMI performance classes, such as Win32_PerfFormattedData_PerfDisk_LogicalDisk or other preinstalled classes derived from Win32_Perf.

The following procedure describes how to refresh data in scripts.

To refresh data in scripts

  1. Call CreateObject to create an SWbemRefresher refresher object.

    Set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
    
  2. Connect to the WMI namespace. To use preinstalled Win32_Perf performances classes, connect to root\cimv2.

    Set objServicesCimv2 = GetObject("winmgmts:\\" _
        & strComputer & "\root\cimv2")
    
  3. Add a single object (call SWbemRefresher.Add) or a collection (call SWbemRefresher.AddEnum) to the refresher.

    Use the precalculated data classes derived from Win32_PerfFormattedData, for example, Win32_PerfFormattedData_PerfDisk_LogicalDisk instead of Win32_PerfRawData_PerfDisk_LogicalDisk. Otherwise, you must calculate the values for all of the properties other than simple counters.

    Set objRefreshableItem = _
        objRefresher.AddEnum(objServicesCimv2 , _
        "Win32_PerfFormattedData_PerfProc_Process")
    
  4. Refresh the data one time to get the initial performance data.

    Call either the SWbemRefresher.Refresh method or the generic SWbemObjectEx.Refresh_ method.

    objRefresher.Refresh
    
  5. If you are monitoring performance and have a collection in the refresher object, loop through the collection objects.

    For Each Process in objRefreshableItem.ObjectSet
        If Process.PercentProcessorTime > 1 then
            WScript.Echo Process.Name & vbnewLine _
                & Process.PercentProcessorTime & "%"
        End If
    Next
    
  6. Clear the items from the refresher by calling SWbemRefresher.DeleteAll or remove specific items by calling SwbemRefresher.Remove.

The following VBScript code example shows how to refresh a single object on the local computer. The script creates a refresher container and adds an instance of an enumerator for Win32_PerfFormattedData_PerfProc_Process instances. The Refresh call is made three times to demonstrate the changes in the PercentProcessorTime property for processes using more than one percent of the processor time.

On Error Resume Next
strComputer = "."
Set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
Set objServicesCimv2 = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
If Err = 0 Then 
Set objRefreshableItem = _
    objRefresher.AddEnum(objServicesCimv2 ,"Win32_PerfFormattedData_PerfProc_Process")
objRefresher.Refresh
' Loop through the processes three times to locate  
'    and display all the process currently using 
'    more than 1 % of the process time. Refresh on each pass.
For i = 1 to 3
    Wscript.Echo "Refresh number " & i 
    objRefresher.Refresh 
    For Each Process in objRefreshableItem.ObjectSet
        If Process.PercentProcessorTime > 1 then
            WScript.Echo Process.Name & vbnewLine & Process.PercentProcessorTime & "%"
        End If
    Next
Next
Else
    WScript.Echo Err.Description
End If

The Index property of the returned SWbemRefreshableItem represents the index of the object in the refresher collection. You can call SWbemRefreshableItem.IsSet property to determine whether or not an item in a refresher is a single item or a collection. To access a single item, use the SWbemRefreshableItem.Object property. If you do not make the call to SWbemRefreshableItem.Object, then the script fails when you try to access the object. To access a collection, use the SWbemRefreshableItem.ObjectSet property.

Performance Counter Classes

Accessing Performance Data in Script

WMI Tasks: Performance Monitoring

Monitoring Performance Data