Created
September 20, 2022 18:48
-
-
Save bill-long/8b64dd298e8825fff3d9fc8fe95999d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
param ( | |
[Parameter()] | |
[int] | |
$PercentCPUThreshold = 80, | |
[Parameter()] | |
[int] | |
$DurationMilliseconds = 10000 | |
) | |
$coresOverThreshold = New-Object 'System.Collections.Generic.Dictionary[string, long]' | |
$sw = New-Object System.Diagnostics.Stopwatch | |
$sw.Start() | |
Get-Counter "\Processor(*)\% Processor Time" -Continuous | ForEach-Object { | |
$_.CounterSamples | ForEach-Object { | |
if ($_.CookedValue -lt $Threshold) { | |
if ($coresOverThreshold.ContainsKey($_.InstanceName)) { | |
[void]$coresOverThreshold.Remove($_.InstanceName) | |
} | |
} else { | |
if ($coresOverThreshold.ContainsKey($_.InstanceName)) { | |
$coreOverThresholdDuration = $sw.ElapsedMilliseconds - $coresOverThreshold[$_.InstanceName] | |
if ($coreOverThresholdDuration -gt $Duration) { | |
Write-Host "Core $($_.InstanceName) has been over $Threshold% for $coreOverThresholdDuration ms" | |
} | |
} else { | |
[void]$coresOverThreshold.Add($_.InstanceName, $sw.ElapsedMilliseconds) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment