如果你SELECT * FROM Win32_PerfFormattedData_Counters_ProcessorInformation
那么你会得到一个结果集的列表。第一组是所有 CPU 的总体值,其余的是各个 CPU 的值。
这段代码显示了这个想法。请注意,您应该替换SYSTEM
为您要查询的计算机的名称。
use strict;
use warnings;
use Win32::OLE;
use Win32::OLE::Variant;
STDOUT->autoflush;
my $wmi = Win32::OLE->GetObject('winmgmts:\\\\SYSTEM\root\cimv2') or die Win32::OLE->LastError;
my $list = $wmi->ExecQuery('SELECT * FROM Win32_PerfFormattedData_Counters_ProcessorInformation');
my $n = 0;
for my $cpu (in $list) {
printf "%s: %d%%\n", $n ? "CPU$n" : ' ALL', $cpu->PercentProcessorTime;
$n++;
}
输出
ALL: 8%
CPU1: 8%
CPU2: 12%
CPU3: 6%
CPU4: 12%
CPU5: 6%
CPU6: 6%
CPU7: 6%