-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathProcess Lifetime CPU percentage.ps1
155 lines (128 loc) · 6.8 KB
/
Process Lifetime CPU percentage.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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"
}
$results
# SIG # Begin signature block
# MIINRQYJKoZIhvcNAQcCoIINNjCCDTICAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUbDyJZ09m2M1a35Ax212PZ1aP
# KtqgggqHMIIFMDCCBBigAwIBAgIQBAkYG1/Vu2Z1U0O1b5VQCDANBgkqhkiG9w0B
# AQsFADBlMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYD
# VQQLExB3d3cuZGlnaWNlcnQuY29tMSQwIgYDVQQDExtEaWdpQ2VydCBBc3N1cmVk
# IElEIFJvb3QgQ0EwHhcNMTMxMDIyMTIwMDAwWhcNMjgxMDIyMTIwMDAwWjByMQsw
# CQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3d3cu
# ZGlnaWNlcnQuY29tMTEwLwYDVQQDEyhEaWdpQ2VydCBTSEEyIEFzc3VyZWQgSUQg
# Q29kZSBTaWduaW5nIENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA
# +NOzHH8OEa9ndwfTCzFJGc/Q+0WZsTrbRPV/5aid2zLXcep2nQUut4/6kkPApfmJ
# 1DcZ17aq8JyGpdglrA55KDp+6dFn08b7KSfH03sjlOSRI5aQd4L5oYQjZhJUM1B0
# sSgmuyRpwsJS8hRniolF1C2ho+mILCCVrhxKhwjfDPXiT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