我正在尝试编写一个脚本,为超级虚拟处理器输出性能计数器的后30个值。
参考:https://blogs.technet.microsoft.com/neales/2016/10/24/hyper-v-performance-cpu/ ->虚拟机处理器使用情况,以及Hyper V机器上每个VM的使用情况。
我试图移动回声,但是它并没有给我所有好的价值:( 谁能和我一起发光?
$vms = Get-VM | Where { $_.State –eq ‘Running’ }
Foreach ( $vm in $vms ) {
Write-Host 'Status: {0} `n ---------- `n' $vm.Name
$processors = Get-VMProcessor -VMName $vm.Name
Foreach ( $processor in $processors ) {
Write-Host '{0}: `n' $processor.Count
$procCount = $processor.Count
for($i = 0 ; $i -le $procCount; $i++){
$fullcounter = "\\" + $env:computername + "\Hyper-V Hypervisor Virtual Processor(" + $vm.Name + ":Hv VP " + $i + ")\% Guest Run Time"
$ret = Get-Counter -Counter $fullcounter -SampleInterval 1 -MaxSamples 30 `
| Select-Object -ExpandProperty CounterSamples `
| Group-Object -Property InstanceName `
| ForEach-Object {
$_ | Select-Object -Property Name, @{n='Average';e= {($_.Group.CookedValue | Measure-Object -Average).Average}};
}
Write-Host "Counter average: $ret"
}
}
}
我得到的输出是:
Status: {0} `n ---------- `n CenTOS
{0}: `n 2
Counter average: @{Name=centos:hv vp 0; Average=13.1989705068454}
Counter average: @{Name=centos:hv vp 1; Average=12.9653966370857}
Counter average:
Status: {0} `n ---------- `n S-Zabbix
{0}: `n 4
Counter average: @{Name=s-zabbix:hv vp 0; Average=5.86643551294767}
Counter average: @{Name=s-zabbix:hv vp 1; Average=1.59246232101088}
Counter average: @{Name=s-zabbix:hv vp 2; Average=1.06161183102294}
Counter average: @{Name=s-zabbix:hv vp 3; Average=1.16016178713955}
Centos有2个CPU,并且还给0和1,这是可以的。但是1之后我得到
Get-Counter : No data to return.
At line:20 char:18
+ ... $ret = Get-Counter -Counter $fullcounter -SampleInterval 1 -MaxS ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidResult: (:) [Get-Counter], Exception
+ FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand
数字3之后的zabbix也一样。
我几乎是剧院。
答案 0 :(得分:0)
如果您要查找的只是计算机上运行的每个管理程序的最后30个用法样本,则可以大大简化此代码。代替示例中使用的基于C的逻辑,让我们尝试基于虚拟机的数据轴对输出进行脚本编写,并有条不紊地遍历每个虚拟机。 (一种更面向对象的方法,适合Powershell。)
如果您想同时全部获取这些数据,我认为它将需要某种并行处理,这是我以前从未使用过的。也许其他人可以解决这个问题。
以下脚本(如果上面的脚本在类似的上下文中起作用)将依次循环通过本地计算机上的每个管理程序,然后通过每个处理器核心,然后打印出计数器值(如果计数器配置正确) )。
需要使用this来获取Get-Counter命令的行为。
$vms = Get-VM | Where { $_.State –eq ‘Running’ }
Foreach ( $vm in $vms ) {
Write-Host 'Status: {0} `n ---------- `n' -f $vm.Name
$processors = Get-VMProcessor -VMName $vm.Name
Foreach ( $processor in $processors ) {
Write-Host '{0}: `n' -f $processor.Name #supposedly. Might need to change this
$fullcounter = "\\" + $env:computername + "\Hyper-V Hypervisor Virtual Processor(" + $vm.Name + ":Hv VP " + $processor.IndexOf($processors) + ")\% Guest Run Time"
Foreach ( $i in 1..30 ) {
$ret = Get-Counter -Counter $fullcounter -SampleInterval 1 -MaxSamples 30 `
| Select-Object -ExpandProperty CounterSamples `
| Group-Object -Property InstanceName `
| ForEach-Object {
$_ | Select-Object -Property Name, @{n='Average';e= {($_.Group.CookedValue | Measure-Object -Average).Average}};
}
Write-Host "Counter average: $ret"
}
}
}
需要测试。
已针对显示进行了优化。如果您希望脚本返回一个对象,建议您使用Class HypervisorUnit { }
之类的东西。
如果我误解了您的要求,请告诉我。
答案 1 :(得分:0)
解决方案:
$vms = Get-VM | Where { $_.State –eq ‘Running’ }
Foreach ( $vm in $vms ) {
Write-Host 'Status: {0} `n ---------- `n' $vm.Name
$processors = Get-VMProcessor -VMName $vm.Name
Foreach ( $processor in $processors ) {
Write-Host '{0}: `n' $processor.Count
$procCount = $processor.Count
for($i = 0 ; $i -le $procCount-1; $i++){
$fullcounter = "\\" + $env:computername + "\Hyper-V Hypervisor Virtual Processor(" + $vm.Name + ":Hv VP " + $i + ")\% Guest Run Time"
$ret = Get-Counter -Counter $fullcounter -SampleInterval 1 -MaxSamples 30 `
| Select-Object -ExpandProperty CounterSamples `
| Group-Object -Property InstanceName `
| ForEach-Object {
$_ | Select-Object -Property Name, @{n='Average';e= {($_.Group.CookedValue | Measure-Object -Average).Average}};
}
Write-Host "Counter average: $ret"
}
}
}
结果:
Status: {0} `n ---------- `n CenTOS
{0}: `n 2
Counter average: @{Name=centos:hv vp 0; Average=13.4647683016234}
Counter average: @{Name=centos:hv vp 1; Average=13.8969682205744}
Status: {0} `n ---------- `n S-Zabbix
{0}: `n 4
Counter average: @{Name=s-zabbix:hv vp 0; Average=1.26615923294145}
Counter average: @{Name=s-zabbix:hv vp 1; Average=5.69107385267803}
Counter average: @{Name=s-zabbix:hv vp 2; Average=1.14422252254857}
Counter average: @{Name=s-zabbix:hv vp 3; Average=1.44512414780457}