Search code examples
c#powershellwmi

Retrieve formatted performance data from WMI


I'm writing a WMI Provider and I've managed to retrieve all info on the Computer System and Hardware Classes but cannot get data I want from the Performance Counter Classes. (Win32 Classes)

Looking through the MSDN documentation and using examples they provide, I've come up with a shell script that should return all properties of the Win32_PerfFormattedData abstract base class.

Script:

$osClass = New-Object System.Management.ManagementClass Win32_PerfFormattedData
$osClass.Options.UseAmendedQualifiers = $true  

# Get the Properties in the class  
$properties = $osClass.Properties  
"This class has {0} properties as follows:" -f $properties.count  


# display the Property name, description, type, qualifiers and instance values  

foreach ($property in $properties) {  
    "Property Name: {0}" -f $property.Name  
    "Description:   {0}" -f $($property.Qualifiers["Description"].Value)  
    "Type:          {0}" -f $property.Type  
    "-------"  
}

(referenced from here)

The problem is that I'm only retrieving the properties from it's base class Win32_Perf

EDIT

After doing more research, I found this on MSDN:

The WMI formatted class name for a counter object is of the form "Win32_PerfFormattedData_service_name_object_name"

I am trying to get the service_name's that are in the Win32_PerfFormattedData and the object_name's within those services.

I'm unsure whether getting the properties is how I want to go about this now but I cannot find any documentation to get the services. Are they the same thing? And, if not, how can I get the info I require? (service_name's & object_name's)

I've also tried this in some C# code and get the same result:

ManagementClass processClass = new ManagementClass();
processClass.Path = new ManagementPath("Win32_PerfFormattedData");

PropertyDataCollection properties = processClass.Properties;

Console.WriteLine("\nProperties:");
foreach (PropertyData property in properties)
{
    Console.WriteLine(property.Name);
}

And I tried retrieving the methods to check if that is what I wanted but nothing is returned:

Console.WriteLine("Methods: ");
foreach (MethodData method in methods)
{
    Console.WriteLine(method.Name);
}

EDIT 2

Is there another way to retrieve this data? I've looked all through the MSDN documentation on the WMI and I think to get the information I want, I have to access that class (Win32_PerfFormattedData). Sorts of values I want to retrieve:

  • CPU
  • RAM
  • Drives (SSD/HDD)
  • Processes
  • OS
  • GPU

I've retrieved a few classes that will give basic information about some of these but will not provide up to date data on, for example, the temperature of each logical processor. I've managed to get 1 service from the class Win32_PerfFormattedData_PerfOS_Processor which provides the load % of each logical processor but that class must holds other services which I need.


Solution

  • Win32_PerfFormattedData_* classes are located under the "root\cimv2" namespace. To enumerate these classes (and get the service names) you run the following WQL query against that namespace:

    SELECT * FROM meta_class WHERE __Class LIKE "Win32_PerfFormattedData%"
    

    Actually you can omit the namespace (at least with ManagementObjectSearcher) in which case the search occurs everywhere. Here is how to search through WMI with C#:

    void SearchWmi()
    {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM meta_class WHERE __Class LIKE \"Win32_PerfFormattedData%\"");
            foreach (ManagementClass wmiClass in searcher.Get())
            {
                  Console.WriteLine(wmiClass["__CLASS"].ToString());
            }
    }
    

    You need to add referece to System.Management as well as the corresponding using directive.

    You could find performance data about the:

    CPU: Win32_PerfFormattedData_Processor

    RAM: Win32_PerfFormattedData_Memory

    OS: Win32_PerfFormattedData_System

    Drives: Win32_PerfFormattedData_PerfDisk_*

    Processes: Win32_PerfFormattedData_PerfProc_*

    I have no idea about the GPU. Most likely it is driver dependent.

    There are numerous WMI explorer tools out there with a UI and all the good stuff. Have you tried some? I use the "WMI Explorer 2.0"

    You can download it from here