Skip to content

Commit

Permalink
Get % of process life that it has consumed CPU
Browse files Browse the repository at this point in the history
Use as an additional CPU hog identifier along with task manager
  • Loading branch information
guyrleech authored Jun 30, 2021
1 parent 6a20bee commit 396c2d0
Showing 1 changed file with 155 additions and 0 deletions.
155 changes: 155 additions & 0 deletions Process Lifetime CPU percentage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@

<#
.SYNOPSIS
Add process property which is the percentage of time that the process has been consuming CPU across it's lifetime.
.DESCRIPTION
Designed to help identify CPU hogs
.PARAMETER process
Process name or pattern to report on. If not specified all processes will be included
.PARAMETER includeUsername
Include the user name owning the process. Must be run as administrator on at least PowerShell version 4
.EXAMPLE
& '.\Process Lifetime CPU percentage.ps1' -includeUsername
Calculate the life time CPU percentage for all processes and include the user name of the process owners (run as admin)
The results can be piped into Export-CSV or Out-GridView, eg |select id,name,CPU,username,CPULeech,sessionid|sort CPULeech -Descending|ogv
.EXAMPLE
& '.\Process Lifetime CPU percentage.ps1' -process svchost
Calculate the life time CPU percentage for all svchost processes (run as admin)
.NOTES
Modification History:
30/06/2021 @guyrleech First public release
#>

[CmdletBinding()]

Param
(
[string]$process ,
[switch]$includeUsername
)

[hashtable]$getprocessparameters = @{ }

if( $PSBoundParameters[ 'process' ] )
{
$getprocessparameters.Add( 'Name' , $process )
}

if( $includeUsername )
{
$getprocessparameters.Add( 'includeUsername' , $includeUsername )
}

## Get average CPU across life of process

[array]$results = @( Get-Process @getprocessparameters -ErrorAction SilentlyContinue | ForEach `
{
$thisProcess = $_
if( $thisProcess.StartTime )
{
Add-Member -InputObject $thisProcess -MemberType NoteProperty -Name 'CPULeech' -Value ( $thisProcess.CPU / (New-TimeSpan -Start $thisProcess.StartTime).TotalSeconds * 100 ) -PassThru
}
elseif( $thisProcess.Id )
{
Write-Warning -Message "No start time for $($thisProcess.Name) ($($thisProcess.Id))"
}
})

Write-Verbose -Message "Got $($results.Count) processes"

if( ! $results -or ! $results.Count )
{
Write-Warning -Message "No processes found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ZIhvcNAQkDMQwGCisGAQQBgjcCAQQwHAYKKwYBBAGCNwIB
# CzEOMAwGCisGAQQBgjcCARUwIwYJKoZIhvcNAQkEMRYEFB/fHzz1nVBDT4MR12zY
# G3ChdsZGMA0GCSqGSIb3DQEBAQUABIIBABoHeVzbSVnj4QlEwFEHjewGAul9UChK
# xgGfG/emY51xHM4BHUgzPPmpXSGVcP4w26duiWJXwA3bE8Y7KACKv0lotH3crPT+
# lwtyMYRyGh2hVMxDIiNCxVQ+qsTDbeqS/4IAbdaMkkY+LLeJIiO/BTwuWtPt4Z9v
# 52ndIrZD78jBpIkXmXvK3sZI/aQyMvTCSdJ+e7TCNYNAIFQq7AqzbG2AVk7rH/aN
# oLCJVqp6soouwEIRVR2/wau/Er11wD4S+Oat+vrA8NoLXc/OpRQ2bdLnTe46pnqT
# iwXgZhafbm/K7T7bV1mOc17PRVP1N92xMG1Rs8IpbR7amstuc+ig9z4=
# SIG # End signature block

0 comments on commit 396c2d0

Please sign in to comment.