-
Notifications
You must be signed in to change notification settings - Fork 1
/
old - no formatter - Get-ObjectProperty.ps1
205 lines (165 loc) · 6.4 KB
/
old - no formatter - Get-ObjectProperty.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
using namespace System.Collections.Generic
function Get-ObjectProperty {
<#
.synopsis
Nicer output to inspect objects than ($obj.psobject.properties | format-table)
.example
PS> [pscustomobject]@{ 'species'='cat'; 'lives'=9;} | Get-ObjectProperty | Format-Table
Type TypeOfInstance Name Value
---- -------------- ---- -----
[String] [String] species cat
[Int32] [Int32] lives 9
.example
PS> [pscustomobject]@{ 'species'='cat'; 'lives'=9;} | Get-ObjectProperty | Format-List
Type : [String]
TypeOfInstance : [String]
Name : species
Value : cat
Type : [Int32]
TypeOfInstance : [Int32]
Name : lives
Value : 9
.example
PS> [xml]'<Item name="" identifier="spear" category="Equipment" interactthroughwalls="true" cargocontaineridentifier="metalcrate" tags="mediumitem,harpoonammo" Scale="0.5" impactsoundtag="impact_metal_light"></Item>'
| Get-ObjectProperty
| ? Name -in 'NextSibling', 'PreviousSibling', 'identifier'
#output:
Type TypeOfInstance Name Value
---- -------------- ---- -----
[String] [String] LocalName #document
[XmlNode] [␀] PreviousSibling
[XmlNode] [␀] NextSibling
.notes
now defaults to one output per typename
example output:
# example output:
PS> ps | prop | ft
TypeOfInstance Name Value
-------------- ---- -----
String Name Agent
Int32 SI 0
Int32 Handles 495
Int64 VM 176238592
Int64 WS 16920576
Int64 PM 41918464
Int64 NPM 77528
[␀] Path
[␀] CommandLine
[␀] Parent
[␀] Company
[␀] CPU
[␀] FileVersion
[␀] ProductVersion
[␀] Description
[␀] Product
String __NounName Process
#>
[cmdletbinding()]
[Alias('Prop', 'ObjectProperty')]
param(
# any object with properties to inspect
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
[object]$InputObject,
# Return extra information?
[Parameter()][switch]$Detailed
)
begin {
$splat_FormatType = @{
IgnorePrefix = 'System.Xml'
# NoBrackets = $false
}
$Config = @{
SymbolNull = "[`u{2400}]" # [Null]
}
$inputList = [list[object]]::new()
}
process {
$inputList.Add( $InputObject )
}
end {
# Write-Warning 'should not be a raw table'
# Write-Debug 'use: <C:\Users\cppmo_000\Documents\2020\powershell\consolidate\2020-12\custom formatting for property names\Custom format using PsTypeNames on PSCO 2020-12.ps1>'
$inputList
| Get-Unique -OnType
| ForEach-Object {
$curObject = $_
Write-Debug "Object: $($_.GetType().FullName)"
$curObject.psobject.properties | ForEach-Object {
$curProp = $_
Write-Debug "Property: $($curProp.Name)"
$curProp | Format-Table | Out-String | Write-Debug
$ValueIsNull = $null -eq $curProp.Value
if ($ValueIsNull) {
$DisplayedValueType = $Config.SymbolNull
} else {
$DisplayedValueType = $curProp.Value.GetType() | Format-TypeName @splat_FormatType
}
$meta = [ordered]@{
Type = $curProp.TypeNameOfValue | Format-TypeName @splat_FormatType
TypeOfInstance = $DisplayedValueType
Name = $curProp.Name
Value = $curProp.Value
PsTypeName = 'Nin.PropertyList'
}
[pscustomobject]$meta
# $x + 3
# hr | Write-Warning
# $prop.TypeNameOfValue -as 'type' | Format-TypeName
# | Write-Warning
# $prop.TypeNameOfValue -as 'type'
# | Write-Warning
}
}
<# explicit, inline:
if ($false -and 'manual typeinfo') {
$splat_TypeData_PropertyList = @{
TypeName = 'Nin.PropertyList'
Force = $true
DefaultDisplayPropertySet = 'TypeOfInstance', 'Name', 'Value', 'Type'
# DefaultDisplayPropertySet = @('Name', 'Species', 'Age')
DefaultDisplayProperty = 'Name'
# 'DefaultKeyPropertySet' = '??'
}
Update-TypeData @splat_TypeData_PropertyList -Force
}
#>
}
}
if ($false) {
H1 'Prop output:'
Get-ChildItem . | Get-Unique -OnType | Select-Object -First 1 | Prop | Format-Table
(Get-Date) | Prop | Format-Table
Write-Warning 'bug:'
<#
# is not returning properties on itself:
> @(34) | prop
> (34) | Prop | ExpectedToBe Blank
> @(34) | Prop | ExpectedToBe List
# Should be:
> $x.psobject.properties'
#>
}
if ($false -and $DebugTestMode) {
$catHash = @{'a' = 'cat'; age = 9; children = (0..4) }
$catObj = [pscustomobject]$catHash
H1 'Test1] Param'
$gcm = Get-Command Select-Object
$gcm.Parameters | Prop | Format-Table
H1 'Test2] Basic Objects'
$catHash | Prop | Format-Table
$catObj | Prop | Format-Table
}
if ($false -and $DebugTestMode) {
$catHash = @{'a' = 'cat'; age = 9; children = (0..4) }
$catObj = [pscustomobject]$catHash
Label 'hash'
$catHash | Prop # Get-ObjectProperty
Label 'Obj'
$catObj | Prop # Get-ObjectProperty
hr
$gcm = Get-Command Select-Object
$gcm.Parameters | Prop | Format-List
$gcm.Parameters | Prop | Format-Table
$prop = $gcm.psobject.properties | Where-Object Name -EQ Parameters # | % TypeNameOfValue
$prop.TypeNameOfValue -as 'type' | Format-TypeName
}