0

尝试编写一个脚本,以便使用 PowerShell 从一组服务器获取当前 CPU 负载。有没有一种简单的方法可以做到这一点。我正在使用 Get-WmiObject win32_processor | 选择负载百分比。

基本上,我试图获得 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"
}
}
4

1 回答 1

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"
    }
}
于 2013-08-22T13:33:14.860 回答