Skip to content

Instantly share code, notes, and snippets.

@bill-long
Created September 20, 2022 18:48
Show Gist options
  • Save bill-long/8b64dd298e8825fff3d9fc8fe95999d3 to your computer and use it in GitHub Desktop.
Save bill-long/8b64dd298e8825fff3d9fc8fe95999d3 to your computer and use it in GitHub Desktop.
[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