Repository navigation
Expand file tree
/
Copy pathShow-Threads.ps1
More file actions
65 lines (51 loc) · 2.06 KB
/
Copy pathShow-Threads.ps1
File metadata and controls
65 lines (51 loc) · 2.06 KB
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
<#
.SYNOPSIS
Render or return the current thread hierarchy and status.
.DESCRIPTION
`Show-Threads` builds a complete thread tree via `Get-ThreadTree` and either
returns the structured tree objects or renders them as a formatted hierarchy
string and emits it to the logging system -- depending on the `-AsObject`
switch.
.PARAMETER AsObject
| Name | Type | Description | Default |
|----------|------------------------|---------------------------------------------------|---------|
| AsObject | System.SwitchParameter | Return the tree as PSObjects instead of rendering | $false |
.OUTPUTS
System.Array when `-AsObject` is set. Returns the root-level thread nodes
produced by `Get-ThreadTree`, each with a `Children` array for the full
hierarchy.
None otherwise. Writes a formatted thread hierarchy string to the logging
queue.
.EXAMPLE
PS> Show-Threads
.EXAMPLE
PS> $tree = Show-Threads -AsObject
PS> $tree[0].Children
.NOTES
Relies on `Get-ThreadTree` to build the hierarchy and `Write-ThreadTree` to
render individual branches.
SPDX-FileCopyrightText: 2026 Icinga GmbH <https://icinga.com>
SPDX-License-Identifier: GPL-3.0-or-later
#>
function Show-Threads()
{
param (
[switch]$AsObject = $false
);
[array]$Tree = Get-ThreadTree;
[array]$ThreadIdList = (Get-GlobalRuntimeVariable).Public.Threading.Threads.PSobject.Properties.Name;
if ($AsObject) {
return $Tree;
}
$StringBuilder = New-Object System.Text.StringBuilder;
# Add a blank line for better readability at the beginning
$StringBuilder.AppendLine('') | Out-Null;
$StringBuilder.AppendLine('=== THREAD HIERARCHY ===') | Out-Null;
foreach ($node in $Tree) {
Write-ThreadTree -Node $node -StringBuilder $StringBuilder;
$StringBuilder.AppendLine('') | Out-Null;
}
$StringBuilder.AppendLine("Total Threads: $($ThreadIdList.Count)") | Out-Null;
$StringBuilder.AppendLine("==================") | Out-Null;
Write-LoggingEntry -Message $StringBuilder.ToString() -Level 'Output';
}