Skip to content

Instantly share code, notes, and snippets.

@tadcrazio
Last active May 29, 2017 23:47
Show Gist options
  • Save tadcrazio/29792ffdfcdb6cea52b9b03c0d369566 to your computer and use it in GitHub Desktop.
Save tadcrazio/29792ffdfcdb6cea52b9b03c0d369566 to your computer and use it in GitHub Desktop.
Get CPU usage and top processes
Function Get-CpuUsage
{
[cmdletbinding()]
Param()
$TopCPU = Get-Process | Sort-Object CPU -desc | Select-Object -first 10 | Format-Table -AutoSize CPU, ProcessName
$avg = Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Foreach {$_.Average}
if ($avg -ge 0)
{
$CpuStatus = "OK"
}
elseif ($avg -ge 55 )
{
$CpuStatus = "Warning"
}
else
{
$CpuStatus = "Critical"
}
$data = [pscustomobject] [ordered] @{
Status = $CpuStatus
AverageCpu = $avg
}
$Data | Format-Table -AutoSize | Out-String
$TopCPU | Format-Table -AutoSize | Out-String
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment