Posted in
Windows Powershell,
Windows Server |
No Comment | 3,054 views | 09/07/2013 17:37
This will give you host and guest CPU usage on your Hyper-V host.
1
2
3
4
5
6
7
| $ClusterNode = "Host01"
While ($True)
{
$ChildCPU = ((Get-Counter -ComputerName $ClusterNode -Counter "\Hyper-V Hypervisor Logical Processor(_Total)\% Total Run Time" -MaxSamples 3).CounterSamples | Measure-Object CookedValue -Max).Maximum
$ParentCPU = ((Get-Counter -ComputerName $ClusterNode -Counter "\Processor Information(_Total)\% Processor Time" -MaxSamples 3).CounterSamples | Measure-Object CookedValue -Max).Maximum
Start-Sleep 5
} |
$ClusterNode = "Host01"
While ($True)
{
$ChildCPU = ((Get-Counter -ComputerName $ClusterNode -Counter "\Hyper-V Hypervisor Logical Processor(_Total)\% Total Run Time" -MaxSamples 3).CounterSamples | Measure-Object CookedValue -Max).Maximum
$ParentCPU = ((Get-Counter -ComputerName $ClusterNode -Counter "\Processor Information(_Total)\% Processor Time" -MaxSamples 3).CounterSamples | Measure-Object CookedValue -Max).Maximum
Start-Sleep 5
}
You should change $ClusterNode to start monitoring.
Posted in
Virtual Machine Manager,
Windows Powershell |
No Comment | 7,679 views | 22/10/2010 10:59
In one of the Technet question, John said:
“When the Hyper-V role is enabled, the physical machine becomes a virtual machine known as the parent partition. The virtual machines are called child partitions. Each partition is isolated from the other. Task Manager in any partition shows only the load on the cpus in that partitions. If the child partitions are heavily taxing the physical cpus, Task Manager in the parent partition shows little cpu activity (assuming you don’t have some heavy load in the parent, which you shouldn’t – best practice is to not have things running in the parent).”
So that means, looking task manager is not a way to know exact resource usage of Hyper-V. If you need to monitor CPU usages of child partitions (virtual machines), you should use Perfmon or.. yes, Powershell :) I wrote a little script to see CPU usages. In my environment, we use Windows Server 2008 with Powershell v1, so I have to write it in version 1. But If you have Powershell v2, you surely can use Get-Counter command.
Function HyperVCPU($InstanceName)
{
$CategoryName = "Hyper-V Hypervisor Virtual Processor"
$CounterName = "% Guest Run Time"
$PerfCounter = New-Object System.Diagnostics.PerformanceCounter($CategoryName, $CounterName, $InstanceName)
$Result = $PerfCounter.NextValue()
$Result = $PerfCounter.NextValue()
$Result
}
Function ShowPerfmon
{
$PerfMon = New-Object System.Diagnostics.PerformanceCounterCategory
$PerfMon.CategoryName = "Hyper-V Hypervisor Virtual Processor"
$Instances = $PerfMon.GetInstanceNames()
Foreach ($Instance in $Instances)
{
$CPU = HyperVCPU $Instance
$Results = New-Object Psobject
$Results | Add-Member Noteproperty Name $Instance
$Results | Add-Member Noteproperty CPU $CPU
$Results
}
}
ShowPerfmon |
Function HyperVCPU($InstanceName)
{
$CategoryName = "Hyper-V Hypervisor Virtual Processor"
$CounterName = "% Guest Run Time"
$PerfCounter = New-Object System.Diagnostics.PerformanceCounter($CategoryName, $CounterName, $InstanceName)
$Result = $PerfCounter.NextValue()
$Result = $PerfCounter.NextValue()
$Result
}
Function ShowPerfmon
{
$PerfMon = New-Object System.Diagnostics.PerformanceCounterCategory
$PerfMon.CategoryName = "Hyper-V Hypervisor Virtual Processor"
$Instances = $PerfMon.GetInstanceNames()
Foreach ($Instance in $Instances)
{
$CPU = HyperVCPU $Instance
$Results = New-Object Psobject
$Results | Add-Member Noteproperty Name $Instance
$Results | Add-Member Noteproperty CPU $CPU
$Results
}
}
ShowPerfmon
But be careful. You need to run Powershell as an Administrator to reach perfmon counters. If not, you will get “Category does not exist” error.