Monitoring WMI Data with VBScript

WMI provides a way to access system counter data programmatically, retrieving the same performance metrics displayed in System Monitor through the Perfmon utility. With built-in performance counter classes, scripts and C++ applications can efficiently gather and analyze system performance data.

WMI Performance Counter Classes

In System Monitor, the “NetworkInterface” object corresponds to two WMI classes: Win32_PerfRawData_Tcpip_NetworkInterface for raw data and Win32_PerfFormattedData_Tcpip_NetworkInterface for precalculated (formatted) data. Classes derived from Win32_PerfRawData and Win32_PerfFormattedData require a refresher object. When working with raw data classes, a C++ application or script must calculate values to match Perfmon.exe outputs. In contrast, formatted data classes provide precalculated values.

VBScript Example: Retrieving Process Performance Data

The following VBScript example uses Win32_PerfFormattedData_PerfProc_Process to retrieve performance data for the Idle process. It displays the % Processor Time counter for the Process object, similar to what appears in Perfmon. The SWbemObjectEx.Refresh_ method ensures the data is updated. At least one refresh operation is required to establish a baseline.

VBScript Code:

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

Monitoring System Performance with WMI Data Providers

WMI includes built-in providers that track system performance both locally and remotely. The WmiPerfClass provider generates classes derived from Win32_PerfRawData and Win32_PerfFormattedData, while the WmiPerfInst provider dynamically supplies data to both raw and formatted classes.

Using Formatted Performance Data Classes

The following VBScript retrieves performance metrics for memory, disk partitions, and server work queues, assessing whether the values fall within acceptable limits.

This script utilizes:

VBScript Code:

Set objCimv2 = GetObject(“winmgmts:root\cimv2”)

Set objRefresher = CreateObject(“WbemScripting.SWbemRefresher”)

‘ Add items to the SWbemRefresher

‘ The SWbemRefreshableItem.ObjectSet call is required for the script to function

Set objMemory = objRefresher.AddEnum _  

    (objCimv2, “Win32_PerfFormattedData_PerfOS_Memory”).ObjectSet

Set objDiskQueue = objRefresher.AddEnum _  

    (objCimv2, “Win32_PerfFormattedData_PerfDisk_LogicalDisk”).ObjectSet

Set objQueueLength = objRefresher.AddEnum _  

    (objCimv2, “Win32_PerfFormattedData_PerfNet_ServerWorkQueues”).ObjectSet

‘ Perform initial refresh to establish baseline values

objRefresher.Refresh

intTotalHealth = 0

‘ Conduct three refresh cycles to collect performance data

For i = 1 to 3

    WScript.Echo “Refresh ” & i

    ‘ Check available memory

    For each intAvailableBytes in objMemory

        WScript.Echo “Available memory (MB): ” & intAvailableBytes.AvailableMBytes

        If intAvailableBytes.AvailableMBytes < 4 Then

            intTotalHealth = intTotalHealth + 1

        End If

    Next

    ‘ Check disk queue length

    For each intDiskQueue in objDiskQueue

        WScript.Echo “Disk queue length – ” & intDiskQueue.Name & “: ” & intDiskQueue.CurrentDiskQueueLength

        If intDiskQueue.CurrentDiskQueueLength > 2 Then

            intTotalHealth = intTotalHealth + 1

        End If

    Next

    ‘ Check server work queue length

    For each intServerQueueLength in objQueueLength

        WScript.Echo “Server work queue length: ” & intServerQueueLength.QueueLength

        If intServerQueueLength.QueueLength > 4 Then

            intTotalHealth = intTotalHealth + 1                       

        End If

    Next

    ‘ Output system health status

    If intTotalHealth > 0 Then

        WScript.Echo “Unhealthy.”

    Else

        WScript.Echo “Healthy.”

    End If

    intTotalHealth = 0

    WScript.Sleep 5000

    ‘ Refresh data for all objects in the collection

    objRefresher.Refresh

Next

Retrieving and Calculating Raw Processor Time with WMI

The following VBScript retrieves the raw PercentProcessorTime data from the Win32_PerfRawData_PerfOS_Processor class on the local system and converts it into a percentage. This script demonstrates how to extract raw performance data and apply the necessary formula to obtain meaningful results.

To determine the percent processor time, refer to the CounterType qualifier for the PercentProcessorTime property. The constant name associated with this counter can be found in the CounterType Qualifier table. By locating the corresponding constant in Counter Types, you can obtain the correct formula for the calculation.

VBScript Code:

Set objService = GetObject( _  

    “Winmgmts:{impersonationlevel=impersonate}!\Root\Cimv2”)

For i = 1 to 8

    ‘ Retrieve initial processor time values

    Set objInstance1 = objService.Get( _  

        “Win32_PerfRawData_PerfOS_Processor.Name=’_Total'”)

    N1 = objInstance1.PercentProcessorTime

    D1 = objInstance1.TimeStamp_Sys100NS

    ‘ Pause for two seconds

    WScript.Sleep(2000)

    ‘ Retrieve updated processor time values

    Set perf_instance2 = objService.Get( _  

        “Win32_PerfRawData_PerfOS_Processor.Name=’_Total'”)

    N2 = perf_instance2.PercentProcessorTime

    D2 = perf_instance2.TimeStamp_Sys100NS

    ‘ Calculate % Processor Time using the PERF_100NSEC_TIMER_INV formula:

    ‘ (1 – ((N2 – N1) / (D2 – D1))) x 100

    PercentProcessorTime = (1 – ((N2 – N1) / (D2 – D1))) * 100

    ‘ Display the result

    WScript.Echo “% Processor Time =”, Round(PercentProcessorTime, 2)

Next

This script collects two processor time values at different intervals, applies the PERF_100NSEC_TIMER_INV formula, and calculates the CPU usage percentage over time.

Conclusion

Using WMI and VBScript, system performance data can be retrieved and analyzed effectively. Performance counter classes provide access to both raw and formatted data, enabling real-time monitoring of key metrics such as memory usage, disk activity, and processor load. By incorporating refresher objects, scripts can ensure updated performance data, while calculations on raw data allow for precise measurements. These methods help in assessing system health and optimizing resource utilization.

Alex Carter

Alex Carter

Alex Carter is a cybersecurity enthusiast and tech writer with a passion for online privacy, website performance, and digital security. With years of experience in web monitoring and threat prevention, Alex simplifies complex topics to help businesses and developers safeguard their online presence. When not exploring the latest in cybersecurity, Alex enjoys testing new tech tools and sharing insights on best practices for a secure web.