1+ Register-PSFTeppScriptblock - Name PSUtil- Input- Object - ScriptBlock {
2+ [System.Management.Automation.Language.PipelineAst ]$pipelineAst = $commandAst.parent
3+ $index = $pipelineAst.PipelineElements.IndexOf ($commandAst )
4+
5+ # region If it's the first command
6+ if ($index -lt 1 )
7+ {
8+ return
9+ }
10+ # endregion If it's the first command
11+
12+ $properties = @ ()
13+ $constraintsPositive = @ ()
14+
15+ # region Process pre-commands
16+ $inputIndex = $index - 1
17+ :main while ($true )
18+ {
19+ if ($pipelineAst.PipelineElements [$inputIndex ].CommandElements)
20+ {
21+ # Resolve command and fail if it breaks
22+ $command = Get-Command $pipelineAst.PipelineElements [$inputIndex ].CommandElements[0 ].Value - ErrorAction Ignore
23+ if ($command -is [System.Management.Automation.AliasInfo ]) { $command = $command.ResolvedCommand }
24+ if (-not $command ) { break }
25+
26+ switch ($command.Name )
27+ {
28+ ' Where-Object' { $inputIndex = $inputIndex - 1 ; continue main }
29+ ' Tee-Object' { $inputIndex = $inputIndex - 1 ; continue main }
30+ # region Select-Object
31+ ' Select-Object'
32+ {
33+ $firstAst = $pipelineAst.PipelineElements [$inputIndex ].CommandElements | Where-Object { $_ -is [System.Management.Automation.Language.ArrayLiteralAst ] } | Select-Object - First 1
34+
35+ foreach ($element in $firstAst.Elements )
36+ {
37+ switch ($element.GetType ().FullName)
38+ {
39+ ' System.Management.Automation.Language.StringConstantExpressionAst'
40+ {
41+ $constraintsPositive += $element.Value
42+ if ($element.Value -notmatch " \*" ) { $properties += $element.Value }
43+ }
44+ ' System.Management.Automation.Language.HashtableAst'
45+ {
46+ $constraintsPositive += ($element.KeyValuePairs | Where-Object Item1 -Match ' ^N$|^Name$' | Select-Object - First 1 ).Item2.ToString().Trim(' "' )
47+ $properties += ($element.KeyValuePairs | Where-Object Item1 -Match ' ^N$|^Name$' | Select-Object - First 1 ).Item2.ToString().Trim(' "' )
48+ }
49+ }
50+ }
51+ $inputIndex = $inputIndex - 1 ;
52+ continue main
53+ }
54+ # endregion Select-Object
55+ # region Select-PSFObject
56+ ' Select-PSFObject'
57+ {
58+ $firstAst = $pipelineAst.PipelineElements [$inputIndex ].CommandElements | Where-Object { $_ -is [System.Management.Automation.Language.ArrayLiteralAst ] } | Select-Object - First 1
59+ foreach ($element in $firstAst.Elements )
60+ {
61+ switch ($element.GetType ().FullName)
62+ {
63+ " System.Management.Automation.Language.StringConstantExpressionAst"
64+ {
65+ $par = [PSFramework.Parameter.SelectParameter ]$element.Value
66+ if ($par.Value -match " \*" ) { $constraintsPositive += $par.Value }
67+ else
68+ {
69+ if ($par.Value -is [System.String ])
70+ {
71+ $properties += $par.Value
72+ $constraintsPositive += $par.Value
73+ }
74+ else
75+ {
76+ $properties += $par.Value [" Name" ]
77+ $constraintsPositive += $par.Value [" Name" ]
78+ }
79+ }
80+ }
81+ " System.Management.Automation.Language.HashtableAst"
82+ {
83+ $properties += ($element.KeyValuePairs | Where-Object Item1 -Match ' ^N$|^Name$' | Select-Object - First 1 ).Item2.ToString().Trim(' "' )
84+ $constraintsPositive += ($element.KeyValuePairs | Where-Object Item1 -Match ' ^N$|^Name$' | Select-Object - First 1 ).Item2.ToString().Trim(' "' )
85+ }
86+ }
87+ }
88+ $inputIndex = $inputIndex - 1 ;
89+ }
90+ # endregion Select-PSFObject
91+ default { break main }
92+ }
93+ }
94+
95+ else
96+ {
97+ break
98+ }
99+ }
100+
101+ # Catch moving through _all_ options in the pipeline
102+ if ($inputIndex -lt 0 ) { return $properties }
103+ # endregion Process pre-commands
104+
105+
106+ # region Input from command
107+ if ($pipelineAst.PipelineElements [$inputIndex ].CommandElements)
108+ {
109+ if ($command = Get-Command $pipelineAst.PipelineElements [$inputIndex ].CommandElements[0 ].Value - ErrorAction Ignore)
110+ {
111+ switch ($command.Name )
112+ {
113+ # region Default for commands
114+ default
115+ {
116+ foreach ($type in $command.OutputType.Type )
117+ {
118+ switch ($type.GetType ().FullName)
119+ {
120+ ' System.IO.FileInfo'
121+ {
122+ $properties += ($type.GetMembers (" Instance, Public" ) | Where-Object MemberType -match " Field|Property" ).Name
123+ $properties += ' PSChildName' , ' PSDrive' , ' PSIsContainer' , ' PSParentPath' , ' PSPath' , ' PSProvider' , ' BaseName'
124+ break
125+ }
126+ ' System.IO.DirectoryInfo'
127+ {
128+ $properties += ($type.GetMembers (" Instance, Public" ) | Where-Object MemberType -match " Field|Property" ).Name
129+ $properties += ' PSChildName' , ' PSDrive' , ' PSIsContainer' , ' PSParentPath' , ' PSPath' , ' PSProvider' , ' BaseName' , ' VersionInfo'
130+ break
131+ }
132+ default { $properties += ($type.GetMembers (" Instance, Public" ) | Where-Object MemberType -match " Field|Property" ).Name }
133+ }
134+ }
135+ }
136+ # endregion Default for commands
137+ }
138+ }
139+ }
140+ # endregion Input from command
141+
142+ # region Input from Variable
143+ if ($pipelineAst.PipelineElements [$inputIndex ].Expression -and $pipelineAst.PipelineElements [0 ].Expression[0 ].VariablePath)
144+ {
145+ $properties += ((Get-Variable - Name $pipelineAst.PipelineElements [0 ].Expression[0 ].VariablePath.UserPath - ValueOnly) | Select-Object - First 1 | Get-Member - MemberType Properties).Name
146+ }
147+ # endregion Input from Variable
148+
149+ $properties | Select-Object - Unique | Sort-Object | ForEach-Object {
150+ if (-not $constraintsPositive ) { $_ }
151+ foreach ($constraint in $constraintsPositive )
152+ {
153+ if ($_ -like $constraint )
154+ {
155+ $_
156+ break
157+ }
158+ }
159+ }
160+ }
0 commit comments