1

我目前有这个脚本,我试图显示处理器负载的百分比。它的返回结果和看似正确的报告,但它没有在输出中显示实际的百分比:

脚本:

$Servers = Get-QADComputer -sizelimit 0 | where {$_.Name -like "*MYSERVER*"} | select Name | sort name
# Best practice: avoid magic numbers; readonly variable for 
new-variable -name CPULIMIT -value 75 -option readonly

foreach($Server in $Servers){
$result = Get-WmiObject win32_processor -ComputerName $Server.Name
# TODO: add error handler here in case $server is unavailable
# Compare the wmi query result to the limit constant
if($result.LoadPercentage -le $CPULIMIT){
    # Write a formatted string that contains the server name and current load
    Write-Host $("Less than 75% Processor Load on {0} ({1}%)" -f $server.name, $result.LoadPercentage) -ForegroundColor "Green"
} else {
    # A warning message would be usefull too
    Write-Host $("More than 75% Processor Load on {0} ({1}%)" -f $server.name, $result.LoadPercentage) -ForegroundColor "Red"
}
}

输出:

 Less than 75% CPU Load on MYSERVER1 (%)
 Less than 75% CPU Load on MYSERVER2 (%)
 Less than 75% CPU Load on MYSERVER3 (%)
 Less than 75% CPU Load on MYSERVER4 (%)

如您所见,现在显示了全部 (%)。

任何想法都会受到极大的赞赏。

谢谢

4

1 回答 1

0

$result[0].LoadPercentage 的 Jon Z 调整效果很好。谢谢你。

于 2013-09-04T11:10:02.090 回答