-
Notifications
You must be signed in to change notification settings - Fork 0
/
Show-Object.ps1
310 lines (259 loc) · 9.14 KB
/
Show-Object.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#############################################################################
##
## Show-Object
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################
<#
.SYNOPSIS
Provides a graphical interface to let you explore and navigate an object.
.EXAMPLE
PS > $ps = { Get-Process -ID $pid }.Ast
PS > Show-Object $ps
#>
param(
## The object to examine
[Parameter(ValueFromPipeline = $true)]
$InputObject
)
Set-StrictMode -Version 3
Add-Type -Assembly System.Windows.Forms
## Figure out the variable name to use when displaying the
## object navigation syntax. To do this, we look through all
## of the variables for the one with the same object identifier.
$rootVariableName = dir variable:\* -Exclude InputObject,Args |
Where-Object {
$_.Value -and
($_.Value.GetType() -eq $InputObject.GetType()) -and
($_.Value.GetHashCode() -eq $InputObject.GetHashCode())
}
## If we got multiple, pick the first
$rootVariableName = $rootVariableName| % Name | Select -First 1
## If we didn't find one, use a default name
if(-not $rootVariableName)
{
$rootVariableName = "InputObject"
}
## A function to add an object to the display tree
function PopulateNode($node, $object)
{
## If we've been asked to add a NULL object, just return
if(-not $object) { return }
## If the object is a collection, then we need to add multiple
## children to the node
if([System.Management.Automation.LanguagePrimitives]::GetEnumerator($object))
{
## Some very rare collections don't support indexing (i.e.: $foo[0]).
## In this situation, PowerShell returns the parent object back when you
## try to access the [0] property.
$isOnlyEnumerable = $object.GetHashCode() -eq $object[0].GetHashCode()
## Go through all the items
$count = 0
foreach($childObjectValue in $object)
{
## Create the new node to add, with the node text of the item and
## value, along with its type
$newChildNode = New-Object Windows.Forms.TreeNode
$newChildNode.Text = "$($node.Name)[$count] = $childObjectValue : " +
$childObjectValue.GetType()
## Use the node name to keep track of the actual property name
## and syntax to access that property.
## If we can't use the index operator to access children, add
## a special tag that we'll handle specially when displaying
## the node names.
if($isOnlyEnumerable)
{
$newChildNode.Name = "@"
}
$newChildNode.Name += "[$count]"
$null = $node.Nodes.Add($newChildNode)
## If this node has children or properties, add a placeholder
## node underneath so that the node shows a '+' sign to be
## expanded.
AddPlaceholderIfRequired $newChildNode $childObjectValue
$count++
}
}
else
{
## If the item was not a collection, then go through its
## properties
foreach($child in $object.PSObject.Properties)
{
## Figure out the value of the property, along with
## its type.
$childObject = $child.Value
$childObjectType = $null
if($childObject)
{
$childObjectType = $childObject.GetType()
}
## Create the new node to add, with the node text of the item and
## value, along with its type
$childNode = New-Object Windows.Forms.TreeNode
$childNode.Text = $child.Name + " = $childObject : $childObjectType"
$childNode.Name = $child.Name
$null = $node.Nodes.Add($childNode)
## If this node has children or properties, add a placeholder
## node underneath so that the node shows a '+' sign to be
## expanded.
AddPlaceholderIfRequired $childNode $childObject
}
}
}
## A function to add a placeholder if required to a node.
## If there are any properties or children for this object, make a temporary
## node with the text "..." so that the node shows a '+' sign to be
## expanded.
function AddPlaceholderIfRequired($node, $object)
{
if(-not $object) { return }
if([System.Management.Automation.LanguagePrimitives]::GetEnumerator($object) -or
@($object.PSObject.Properties))
{
$null = $node.Nodes.Add( (New-Object Windows.Forms.TreeNode "...") )
}
}
## A function invoked when a node is selected.
function OnAfterSelect
{
param($Sender, $TreeViewEventArgs)
## Determine the selected node
$nodeSelected = $Sender.SelectedNode
## Walk through its parents, creating the virtual
## PowerShell syntax to access this property.
$nodePath = GetPathForNode $nodeSelected
## Now, invoke that PowerShell syntax to retrieve
## the value of the property.
$resultObject = Invoke-Expression $nodePath
$outputPane.Text = $nodePath
## If we got some output, put the object's member
## information in the text box.
if($resultObject)
{
$members = Get-Member -InputObject $resultObject | Out-String
$outputPane.Text += "`n" + $members
}
}
## A function invoked when the user is about to expand a node
function OnBeforeExpand
{
param($Sender, $TreeViewCancelEventArgs)
## Determine the selected node
$selectedNode = $TreeViewCancelEventArgs.Node
## If it has a child node that is the placeholder, clear
## the placeholder node.
if($selectedNode.FirstNode -and
($selectedNode.FirstNode.Text -eq "..."))
{
$selectedNode.Nodes.Clear()
}
else
{
return
}
## Walk through its parents, creating the virtual
## PowerShell syntax to access this property.
$nodePath = GetPathForNode $selectedNode
## Now, invoke that PowerShell syntax to retrieve
## the value of the property.
Invoke-Expression "`$resultObject = $nodePath"
## And populate the node with the result object.
PopulateNode $selectedNode $resultObject
}
## A function to handle keypresses on the form.
## In this case, we capture ^C to copy the path of
## the object property that we're currently viewing.
function OnKeyPress
{
param($Sender, $KeyPressEventArgs)
## [Char] 3 = Control-C
if($KeyPressEventArgs.KeyChar -eq 3)
{
$KeyPressEventArgs.Handled = $true
## Get the object path, and set it on the clipboard
$node = $Sender.SelectedNode
$nodePath = GetPathForNode $node
[System.Windows.Forms.Clipboard]::SetText($nodePath)
$form.Close()
}
}
## A function to walk through the parents of a node,
## creating virtual PowerShell syntax to access this property.
function GetPathForNode
{
param($Node)
$nodeElements = @()
## Go through all the parents, adding them so that
## $nodeElements is in order.
while($Node)
{
$nodeElements = ,$Node + $nodeElements
$Node = $Node.Parent
}
## Now go through the node elements
$nodePath = ""
foreach($Node in $nodeElements)
{
$nodeName = $Node.Name
## If it was a node that PowerShell is able to enumerate
## (but not index), wrap it in the array cast operator.
if($nodeName.StartsWith('@'))
{
$nodeName = $nodeName.Substring(1)
$nodePath = "@(" + $nodePath + ")"
}
elseif($nodeName.StartsWith('['))
{
## If it's a child index, we don't need to
## add the dot for property access
}
elseif($nodePath)
{
## Otherwise, we're accessing a property. Add a dot.
$nodePath += "."
}
## Append the node name to the path
$nodePath += $nodeName
}
## And return the result
$nodePath
}
## Create the TreeView, which will hold our object navigation
## area.
$treeView = New-Object Windows.Forms.TreeView
$treeView.Dock = "Top"
$treeView.Height = 500
$treeView.PathSeparator = "."
$treeView.Add_AfterSelect( { OnAfterSelect @args } )
$treeView.Add_BeforeExpand( { OnBeforeExpand @args } )
$treeView.Add_KeyPress( { OnKeyPress @args } )
## Create the output pane, which will hold our object
## member information.
$outputPane = New-Object System.Windows.Forms.TextBox
$outputPane.Multiline = $true
$outputPane.ScrollBars = "Vertical"
$outputPane.Font = "Consolas"
$outputPane.Dock = "Top"
$outputPane.Height = 300
## Create the root node, which represents the object
## we are trying to show.
$root = New-Object Windows.Forms.TreeNode
$root.Text = "$InputObject : " + $InputObject.GetType()
$root.Name = '$' + $rootVariableName
$root.Expand()
$null = $treeView.Nodes.Add($root)
## And populate the initial information into the tree
## view.
PopulateNode $root $InputObject
## Finally, create the main form and show it.
$form = New-Object Windows.Forms.Form
$form.Text = "Browsing " + $root.Text
$form.Width = 1000
$form.Height = 800
$form.Controls.Add($outputPane)
$form.Controls.Add($treeView)
$null = $form.ShowDialog()
$form.Dispose()