Export (0) Print
Expand All

WMI Tasks: Performance Monitoring

Use the WMI classes that obtain data from performance counters to access and refresh data about computer performance. For other examples, see the TechNet ScriptCenter at http://www.microsoft.com/technet. For more information, see Performance Libraries and WMI and Monitoring Performance Data.

The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.

The following procedure describes how to run a script.

Aa394597.wedge(en-us,VS.85).gifTo run a script

  1. Copy the code and save it in a file with a .vbs extension. Ensure that your text editor does not add a .txt extension to the file.
  2. Open a command prompt window and navigate to the directory where you saved the file.
  3. Type cscript scriptfile.vbs at the command prompt.

Note  By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript scriptfile.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.

The following table lists script examples that can be used to obtain various types of data from the local computer.

How do I...WMI classes or methods
...obtain the performance counter data that I can see in the Perfmon utility in a script?Use classes that have names that begin with "Win32_PerfFormattedData", for example Win32_PerfFormattedData_PerfProc_Process. Properties with names like PageFileBytes correspond to performance counters you see in Perfmon. The "Win32_PerfFormattedData" classes calculate the final values of counters for you.
...get on-going performance data for a single process, disk drive, and other data?Use the Win32_PerfFormattedData_PerfProc_Process—or the appropriate formatted Performance Counter Class—and the SWbemObjectEx.Refresh_ method. For more information, see Scripting with SWbemObject.

In C++, use IWbemConfigureRefresher::AddObjectByPath and IWbemRefresher::Refresh. For more information, see Monitoring Performance Data.

The following script runs until the computer is restarted, WMI is stopped, or the script is stopped. To stop the script manually, use Task Manager to stop the process. To stop it programmatically, use the Terminate method in the Win32_Process class.


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
set PerfProcess = objWMIService.Get(_
    "Win32_PerfFormattedData_PerfProc_Process.Name='Idle'")

While (True)
    PerfProcess.Refresh_     
    Wscript.Echo PerfProcess.PercentProcessorTime
    Wscript.Sleep 1000
Wend

...get on-going performance data for all processes without repeated polling?Use classes that have names that begin with "Win32_PerfFormattedData" and an SWbemRefresher object. The refresher holds the objects so you do not need to get the collection repeatedly. A minimum of two values are needed to calculate performance data because most counters are rate counters. The first time you display the refresher data it is empty.

The following script runs indefinitely until the computer is rebooted, WMI is stopped, or the script is stopped. To stop the script manually, use Task Manager to stop the process. To stop it programmatically, use the Terminate method in the Win32_Process class.


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
set objRefresher = CreateObject("WbemScripting.Swbemrefresher")
Set objProcessor = objRefresher.AddEnum _
    (objWMIService, _
    "Win32_PerfFormattedData_PerfOS_Processor").ObjectSet

While (True)
    objRefresher.Refresh
        For each RefreshItem in objRefresher
            For each objProcess in RefreshItem.ObjectSet
                Wscript.Echo objProcess.GetObjectText_
            Next
        Next
     Wscript.Sleep 5000
Wend

...get and calculate performance data for processes on Windows 2000?Use the "Win32_PerfRawData" classes, such as Win32_PerfRawData_PerfProc_Process. Get the property data, such as PercentProcessorTime, twice for a specific process. Look up the formula specified in the CounterType qualifier for the property and calculate. The CounterType in the example is PERF_100NSEC_TIMER_INV. For more information, see Monitoring Performance Data.

The following script runs indefinitely until the computer is rebooted, WMI is stopped, or the script is stopped. To stop the script manually, use Task Manager to stop the process. To stop it programmatically, use the Terminate method in the Win32_Process class.


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")

While (True)
    Set object1 = objWMIService.Get( _
    "Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
    N1 = object1.PercentProcessorTime
    D1 = object1.TimeStamp_Sys100NS
    Wscript.Sleep(1000)
    set object2 = objWMIService.Get( _
    "Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
    N2 = object2.PercentProcessorTime
    D2 = object2.TimeStamp_Sys100NS

    ' CounterType - PERF_100NSEC_TIMER_INV
    ' Formula - (1- ((N2 - N1) / (D2 - D1))) x 100
    PercentProcessorTime = (1 - ((N2 - N1)/(D2-D1)))*100

    Wscript.Echo "% Processor Time=" , PercentProcessorTime

Wend

 

Related topics

WMI Tasks for Scripts and Applications
WMI C++ Application Examples
TechNet ScriptCenter

 

 

Show:
© 2014 Microsoft