Powershell内存负载百分比

时间:2013-09-03 12:25:24

标签: powershell scripting powershell-v2.0

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

SCRIPT:

$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 (%)

正如您所看到的,现在已经显示了所有(%)。

任何想法都会大受欢迎。

由于

1 个答案:

答案 0 :(得分:0)

Jon Z调整$ result [0] .LoadPercentage工作得很好。谢谢。