尝试将使用PowerShell从一组服务器获取当前CPU负载的脚本组合在一起。有一个简单的方法来做到这一点。我正在使用Get-WmiObject win32_processor |选择LoadPercentage。
基本上我试图获得CPU负载,如果使用率超过75%则显示红色,如果低于75%,则显示绿色。最好显示负载百分比。
到目前为止,我有这个不完整的,可能是完全错误的:
$Servers = Get-QADComputer -sizelimit 0 | where {$_.Name -like "*myserver*"} | select Name
foreach($Server in $Servers){
$I = $Server.Name
$result = Get-WmiObject win32_processor -ComputerName $Server.Name | select LoadPercentage | ft
if($result -eq $null){
Write-Host $Server.LoadPercentage "Less than 75% CPU Load" -ForegroundColor "Green"
}
}
答案 0 :(得分:0)
假设要使用主机名填充$Servers
集合,您就不那么遥远了。我没有Quest工具,因此无法检查集合。
# Best practice: avoid mistyped variable names
set-psdebug -strict
$Servers = Get-QADComputer -sizelimit 0 | where {$_.Name -like "*myserver*"} | select 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% CPU Load on {0} ({1}%)" -f $server, $result.LoadPercentage) -ForegroundColor "Green"
} else {
# A warning message would be usefull too
Write-Host $("Over than 75% CPU Load on {0} ({1}%)" -f $server, $result.LoadPercentage) -ForegroundColor "Red"
}
}