-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Get-CommandLine.ps1
72 lines (60 loc) · 1.79 KB
/
Get-CommandLine.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
<#
.SYNOPSIS
Reports all processes with Id, Name, and CommandLine and highlights categories of and
optional specified processes.
.PARAMETER Name
A string used to match and highlight entries based on their name.
.PARAMETER Only
Only display matched entries.
.PARAMETER ReturnValue
A switch, if specified as $true, returns the commandline of the specified process rather
than generating a report
.PARAMETER ShowSystem
Show processes running out of Windows\System32 folder; default is to hide these processes
#>
param (
[string] $Name,
[switch] $ShowSystem,
[switch] $Only,
[switch] $ReturnValue)
if (!$Name)
{
$Only = $false
$ReturnValue = $false
}
$format = '{0,10} {1,-33} {2}'
if (!$ReturnValue)
{
Write-Host ($format -f 'processid', 'ProcessName', 'CommandLine')
Write-Host ($format -f '---------', '-----------', '-----------')
}
gcim Win32_Process | sort -Property ProcessName | select ProcessId, ProcessName, CommandLine | % `
{
$procnam = [IO.Path]::GetFileNameWithoutExtension($_.ProcessName)
if ($procnam.Length -gt 33) { $procnam = $procnam.Substring(0,31) + '...' }
$commandLine = $_.CommandLine
if (!$commandLine) { $commandLine = '' }
$cmd = $commandLine
$max = $host.UI.RawUI.WindowSize.Width - 45
if ($cmd.Length -gt $max) { $cmd = $cmd.Substring(0, $max - 3) + '...' }
if ($Name -and ($procnam -like "*$Name*" -or $commandLine -like "*$Name*"))
{
if ($ReturnValue) { return $_.CommandLine }
Write-Host ($format -f $_.ProcessId, $procnam, $cmd) -ForegroundColor Green
if ($Only) { return }
}
elseif (!$Only -and !$ReturnValue)
{
if ($cmd -and ($cmd -like "*$($env:windir)\System32*"))
{
if ($ShowSystem)
{
Write-Host ($format -f $_.ProcessId, $procnam, $cmd) -ForegroundColor DarkGray
}
}
else
{
Write-Host ($format -f $_.ProcessId, $procnam, $cmd)
}
}
}