-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpstree.ps1
49 lines (41 loc) · 1.8 KB
/
pstree.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
$ProcessesById = @{}
foreach ($Process in (Get-WMIObject -Class Win32_Process)) {
$ProcessesById[$Process.ProcessId] = $Process
}
$ProcessesWithoutParents = @()
$ProcessesByParent = @{}
foreach ($Pair in $ProcessesById.GetEnumerator()) {
$Process = $Pair.Value
if (($Process.ParentProcessId -eq 0) -or !$ProcessesById.ContainsKey($Process.ParentProcessId)) {
$ProcessesWithoutParents += $Process
continue
}
if (!$ProcessesByParent.ContainsKey($Process.ParentProcessId)) {
$ProcessesByParent[$Process.ParentProcessId] = @()
}
$Siblings = $ProcessesByParent[$Process.ParentProcessId]
$Siblings += $Process
$ProcessesByParent[$Process.ParentProcessId] = $Siblings
}
function Show-ProcessTree([UInt32]$ProcessId, $IndentLevel) {
$Process = $ProcessesById[$ProcessId]
$Indent = ("." * $IndentLevel)
if ($IndentLevel -eq 0){
$Indent =""
}
$commandline = $Process.CommandLine
$Name = $Indent + $Process.Name
$ExePath = $Process.Executablepath
$owner = $Process.getowner()
$user = $owner.domain + "\" + $owner.user
$creationdate_str = $Process.ConvertToDateTime($Process.CreationDate)
Write-Output ("{0,6} {1,-30} {2,-30} {3} {4} {5}" -f $Process.ProcessId, $Name, $user, $creationdate_str, $ExePath, $commandline)
foreach ($Child in ($ProcessesByParent[$ProcessId] | Sort-Object CreationDate)) {
Show-ProcessTree $Child.ProcessId ($IndentLevel + 2)
}
}
Write-Output ("{0,6} {1} {2} {3} {4}" -f "PID", "Name ", "User ","Creation date ","Image path")
Write-Output ("{0,6} {1} {2} {3} {4}" -f "---", "---------------------------- ","---------------------------- ","-------------------","-----------")
foreach ($Process in ($ProcessesWithoutParents | Sort-Object CreationDate)) {
Show-ProcessTree $Process.ProcessId 0
}