查询多个服务器以获取计数器信息 - PowerShell,Workflow

时间:2014-06-08 09:32:50

标签: powershell hyper-v

我正在尝试查询多个Hyper-V主机以查找当前的CPU& RAM(平均5个样本)和磁盘使用情况。我希望所有查询并行,最终结果是我想选择资源最多的主机来创建VM。

我无法让它与Get-Counter一起使用,因为每个计数器都会按顺序检查,我尝试使用Jobs,但无法使嵌套作业正常工作。所以最后我正在尝试使用Workflows,我遇到的问题是我无法弄清楚如何以有意义的方式存储信息。我试图在哈希表中使用哈希表,但这在工作流中无效,我使用的是:

$HostResources["$Server"] = @{ "CPU"=$CPU; "RAM"=$RAM; "Disk"=$Disk }

我也感觉可能有更好的方法来实现这一点,包括启用Hyper-V计量,但这似乎不会在主机上提供信息,仅在VM上提供。所以如果你认为有更好的方法,请指出正确的方向,我会去研究它。

Workflow Get-Resources {

$Servers = @('host00', 'host01', 'host02', 'host03')
# $HostResources = @{}

ForEach -Parallel ($Server in $Servers) {
    InlineScript { (Get-Counter -Counter "\Processor(_Total)\% Processor Time" -ComputerName $using:Server -SampleInterval 1 -MaxSamples 5).CounterSamples.CookedValue | Measure-Object -Average | Select-Object -ExpandProperty Average }
    InlineScript { (Get-Counter -Counter "\Memory\Available MBytes" -ComputerName $using:Server -SampleInterval 1 -MaxSamples 5).CounterSamples.CookedValue | Measure-Object -Average | Select-Object -ExpandProperty Average }
    InlineScript { (Get-Counter -Counter "\LogicalDisk(_Total)\% Free Space" -ComputerName $using:Server).CounterSamples.CookedValue }
    }
}

$Result = Get-Resources

1 个答案:

答案 0 :(得分:0)

使用Start-Job尝试此实施,您将在$HostResources结尾处获得结果,并可以轻松地向$counters$countersAV添加更多计数器(针对平均计数器) :

$Servers = @('host00', 'host01', 'host02', 'host03')
    foreach($server in $servers){   
        start-job -Name $server -ScriptBlock {param($server)
            $countersAV = @("\Processor(_Total)\% Processor Time","\Memory\Available MBytes")
            $counters = @("\LogicalDisk(_Total)\% Free Space")
            foreach($counterAV in $countersAV){
                echo ($server + $counterAV + ":" + ((Get-Counter -Counter $counterAV -ComputerName $server -SampleInterval 1 -MaxSamples 5).CounterSamples.CookedValue | Measure-Object -Average | Select-Object -ExpandProperty Average))
            }
            foreach($counter in $counters){
                echo ($server + $counter + ":" + ((Get-Counter -Counter $counter -ComputerName $Server).CounterSamples.CookedValue))
            }
        } -ArgumentList $server 
    }   
$HostResources =  (Get-Job | Wait-Job | Receive-Job)
Get-Job | Remove-Job #clean up

结果:

host00\Processor(_Total)\% Processor Time:0.800450654666718
host00\Memory\Available MBytes:5772
host00\LogicalDisk(_Total)\% Free Space:51.8221343719115
host01\Processor(_Total)\% Processor Time:0.386754157141649
host01\Memory\Available MBytes:2717
host01\LogicalDisk(_Total)\% Free Space:67.5283240005209
...