Skip to content

Instantly share code, notes, and snippets.

@zengxinhui
Last active August 1, 2023 14:16
Show Gist options
  • Save zengxinhui/51581e6bb544298610a804b782e75360 to your computer and use it in GitHub Desktop.
Save zengxinhui/51581e6bb544298610a804b782e75360 to your computer and use it in GitHub Desktop.
Simple CPU usage monitoring
$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