-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Get-TopProcess.ps1
63 lines (57 loc) · 2.62 KB
/
Get-TopProcess.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
#requires -version 5.1
#requires -module PSScriptTools
Function Get-TopProcess {
[cmdletbinding()]
Param(
[Parameter(Position = 0)]
[ValidateSet('WorkingSet', 'VM', 'CPU', 'Handles', 'PM')]
[string]$Property = "WorkingSet",
[ValidateRange(1, 25)]
[int32]$Top = 10,
[Parameter(ValueFromPipeline)]
[string]$Computername = $env:computername
)
Begin {
$vlog = New-CustomFileName -Template "$env:temp\$($MyInvocation.MyCommand)-%time.log"
$PSDefaultParameterValues."Out-VerboseTee:Path" = $vlog
$PSDefaultParameterValues."Out-VerboseTee:Append" = $True
$PSDefaultParameterValues."Write-Detail:Prefix" = 'Begin'
Write-Detail "Starting $($MyInvocation.MyCommand)" | Tee-Verbose
Write-Detail "Using verbose log $vlog" | Tee-Verbose
Write-Detail "Execution metadata" | Tee-Verbose
Write-Detail (Add-Border -TextBlock (Get-PSWho -AsString) | Out-String) | Tee-Verbose
Write-Detail "Initializing data array" | Tee-Verbose
} #begin
Process {
$PSDefaultParameterValues."Write-Detail:Prefix" = 'Process'
Write-Detail "Gathering process information from $Computername" | Tee-Verbose
$all += Invoke-Command -ScriptBlock {
Get-Process |
Sort-Object -Property $using:Property -Descending |
Select-Object -First $using:Top
} -ComputerName $computername
} #process
End {
$PSDefaultParameterValues."Write-Detail:Prefix" = 'End'
$all
if ($VerbosePreference -eq 'Continue') {
#save results to the verbose log
#add the detail to the log but don't make it verbose
Write-Detail "Data results" | Add-Content -Path $vlog
$all | Out-String | Add-Content -Path $vlog
#save results for each computer to a separate log
$all | Group-Object -Property PSComputername |
ForEach-Object {
$log = New-CustomFileName -Template "$env:temp\$($_.Name)_TopProcess_$Property-%Year%Month%time-%###.txt"
Write-Detail "Logging output to $log" | Tee-Verbose
$_.Group | Out-String | Set-Content -Path $log
}
}
Write-Detail "Ending $($MyInvocation.MyCommand)" | Tee-Verbose
$PSDefaultParameterValues.Remove("Out-VerboseTee:Path")
$PSDefaultParameterValues.Remove("Out-VerboseTee:Append")
$PSDefaultParameterValues.Remove("Write-Detail:Prefix")
} #end
} #close command
#run the function
'localhost', $env:computername, (hostname) | Get-TopProcess -top 5 -verbose