Düzenle

Aracılığıyla paylaş


Accessing Performance Data in Script

WMI scripts can access the preinstalled WMI Performance Counter Classes, either on the local computer or remotely. While scripts can obtain data from uncalculated classes, such as Win32_PerfRawData_PerfOS_Memory, or formatted classes, Win32_PerfFormattedData_PerfOS_Memory, the formatted data classes can be easier to use.

Monitoring performance data with the performance counter classes requires the use of a refresher. Use the SWbemRefresher object to store one or more performance objects for refresh or refresh a single object by the SWbemObjectEx.Refresh call. For more information, see Refreshing WMI Data in Scripts.

By setting the SWbemRefresher.AutoReconnect property to TRUE, WMI automatically reconnects to a remote provider if the connection is broken so that you do not need to check for connection status.

As shown in the following script code example script, you must make an initial refresh call to get a starting value for the object you are refreshing. Subsequent refresh calls then contain data.

Note

When a script is accessing WMI performance counter data from a remote computer, the script can only run under the current logged-on user account. WMI does not support an SWbemLocator.ConnectServer call that passes in different user credentials. Therefore, the account calling the remote computer must already have the appropriate privileges on that computer.

 

The following script code example shows how to use an SWbemRefresher object to update the data in performance counter objects. For more information about using performance counters in WMI, see Accessing WMI Preinstalled Performance Classes.

' Get raw and cooked data performance counter instances for the
" wscript process running this script

set RawProc = GetObject("winmgmts:Win32_PerfRawdata_Perfproc_process.name='wscript'")
set CookedProc = GetObject("winmgmts:Win32_Perfformatteddata_Perfproc_process.name='wscript'")

' Display the same property in raw and cooked form in a loop
for I = 1 to 6
    Wscript.Echo "wscript process raw PageFaultsPerSec = & RawProc.PageFaultsPerSec _
                 & " cooked PageFaultsPerSec= " & CookedProc.PageFaultsPerSec 

' Wait 2 seconds
    Wscript.Sleep 2000
    
    ' Refresh the object
    RawProc.Refresh_
    CookedProc.Refresh_
next

Example

The following script code example shows that you must make an initial refresh call to get a starting value for the refreshed object. Subsequent refresh calls then contain data.

The following script code example shows how to use an SWbemRefresher object to update the data in performance counter objects. For more information about using performance counters in WMI, see Accessing WMI Preinstalled Performance Classes.

' Get raw and cooked data performance counter instances for the
" wscript process running this script

set RawProc = GetObject("winmgmts:" _
                        & "Win32_PerfRawdata_Perfproc_process." _
                        & "name='wscript'")
set CookedProc = GetObject("winmgmts:" _ 
                & "Win32_Perfformatteddata_Perfproc_process." _
                & "name='wscript'")

' Display the same property in raw and cooked form in a loop
for I = 1 to 6
    Wscript.Echo "wscript process raw PageFaultsPerSec = " _
                 & RawProc.PageFaultsPerSec _
                 & " cooked PageFaultsPerSec= " _
                 & CookedProc.PageFaultsPerSec 

' Wait 2 seconds
    Wscript.Sleep 2000
    
    ' Refresh the object
    RawProc.Refresh_
    CookedProc.Refresh_
next

Performance Counter Classes

WMI Tasks: Performance Monitoring

Monitoring Performance Data