Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help formatting the output of a script....

Status
Not open for further replies.

ewornibor

MIS
Feb 23, 2016
23
US
I need to write a PS script that outputs process tree with child processes nested under the parent processes. It needs to identify all processes with no parent processes as the root for the trees.

I am NOT a PS programmer, not even a little bit so I need assistance please. Found some examples that kinda give me what I am looking for but need some guidance...:

Code:
$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 ($Process.name) {
    $Description =  $Process.name
  } else {
    $Description = $Process.Caption
  }

  Write-Output ("{0,6}{1} {2}" -f $Process.ProcessId, $Indent, $Description)
  foreach ($Child in ($ProcessesByParent[$ProcessId] | Sort-Object CreationDate)) {
    Show-ProcessTree $Child.ProcessId ($IndentLevel + 4)
  }
}

Write-Output ("{0,6} {1}" -f "Id", "Process Name")
Write-Output ("{0,6} {1}" -f "---", "------------")

foreach ($Process in ($ProcessesWithoutParents | Sort-Object CreationDate)) {
  Show-ProcessTree $Process.ProcessId 0
}



Processes need to be identified at the root of trees and nest children under parents. I have attached an example picture of what the output should look like:
Thank you!
 
Are you even getting the correct data to output? Please post what you have in each of your arrays.

And what is this section of code suppose to do? Because about 9 lines above it you declare $ProcessesByParent
as a hashtable.

Code:
if (![COLOR=#EF2929]$ProcessesByParent[/color].ContainsKey($Process.ParentProcessId)) {
    $ProcessesByParent[$Process.ParentProcessId] = @()
  }
  $Siblings = $ProcessesByParent[$Process.ParentProcessId]   [COLOR=#EF2929]\[/color]
  $Siblings += $Process                                       [COLOR=#EF2929]>--- What is this doing?[/color]
  $ProcessesByParent[$Process.ParentProcessId] = $Siblings   [COLOR=#EF2929]/[/color]


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top