Last active
August 1, 2023 14:16
-
-
Save zengxinhui/51581e6bb544298610a804b782e75360 to your computer and use it in GitHub Desktop.
Simple CPU usage monitoring
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
$usageQueue = @() | |
while ($true) { | |
$currentDateTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss" | |
$cpuUsage = Get-Counter '\Processor(_Total)\% Processor Time' | |
$cpuUsageValue = $cpuUsage.CounterSamples.CookedValue | |
$usageQueue += $cpuUsageValue | |
if ($usageQueue.Count -eq 13) { | |
$usageQueue = $usageQueue[1..12] | |
} | |
$oneMinAvg = 0 | |
foreach ($usage in $usageQueue) { | |
$oneMinAvg += $usage | |
} | |
$oneMinAvg /= 12 | |
$cpuUsageValueStr = "{0,6:N2}" -f $cpuUsageValue | |
$oneMinAvgStr = "{0,6:N2}" -f $oneMinAvg | |
Write-Host "$currentDateTime, CPU Utilization: $cpuUsageValueStr%, 1min Avg: $oneMinAvgStr%" | |
Start-Sleep -Seconds 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment