从WMI

时间:2016-06-08 18:31:01

标签: c# powershell wmi

我正在编写WMI提供程序,并且我已设法检索 计算机系统和硬件类 上的所有信息,但无法从 绩效计数器类 。 (Win32 Classes

通过查看MSDN文档并使用它们提供的示例,我提出了一个shell脚本,它应该返回 Win32_PerfFormattedData 抽象基类的所有属性。

脚本:

$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  
    "-------"  
}

(引自here

问题是我只是从它的基类Win32_Perf中检索属性

修改

经过更多研究后,我在MSDN上发现了这个:

  

计数器对象的WMI格式化类名称格式为“Win32_PerfFormattedData_ service_name _ object_name

我正在尝试获取 Win32_PerfFormattedData 中的 service_name 以及这些服务中的 object_name

我不确定是否获取属性是我现在想要的方式,但我找不到任何文档来获取服务。它们是一样的吗?如果没有,我怎样才能得到我需要的信息? ( service_name 的& object_name

我也在一些C#代码中试过这个并获得相同的结果:

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);
}

我尝试检索方法以检查这是否是我想要的但没有返回任何内容:

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

编辑2

还有另一种检索此数据的方法吗?我已经查看了WMI上的MSDN文档,我想要获取我想要的信息,我必须访问该类( Win32_PerfFormattedData )。我想要检索的各种值:

  • CPU
  • RAM
  • 驱动器(SSD / HDD)
  • 过程
  • OS
  • GPU

我已经检索了几个类,这些类将提供有关其中一些的基本信息,但不会提供有关每个逻辑处理器温度的最新数据。我已经设法从类 Win32_PerfFormattedData_PerfOS_Processor 获得1个服务,它提供每个逻辑处理器的负载%,但该类必须包含我需要的其他服务。

1 个答案:

答案 0 :(得分:2)

Win32_PerfFormattedData_*类位于“root \ cimv2”命名空间下。要枚举这些类(并获取服务名称),请对该命名空间运行以下WQL查询:

SELECT * FROM meta_class WHERE __Class LIKE "Win32_PerfFormattedData%"

实际上你可以省略命名空间(至少用ManagementObjectSearcher),在这种情况下搜索到处都是。以下是如何使用C#搜索WMI:

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());
        }
}

您需要添加引用System.Management以及相应的using指令。

您可以找到有关以下内容的效果数据:

CPU:Win32_PerfFormattedData_Processor

RAM:Win32_PerfFormattedData_Memory

操作系统:Win32_PerfFormattedData_System

驱动器:Win32_PerfFormattedData_PerfDisk_*

流程:Win32_PerfFormattedData_PerfProc_*

我不知道GPU。最有可能的是它取决于驾驶员。

有许多WMI资源管理器工具,其中包含用户界面和所有好东西。你试过一些吗?我使用“WMI Explorer 2.0”

您可以从here

下载