I've written a PowerShell (v7.7) script function that outputs information from the Get-Member (GM) cmdlet about the variable (name and value) that is passed into my function.
Here is a sample of the function's output (I added the titles for clarity):
var-name var-value var-type
$v08 Collection [Hashtable]
System.Object Item(System.Object key) {get;set;}
int Count {get;}
bool IsFixedSize {get;}
bool IsReadOnly {get;}
bool IsSynchronized {get;}
System.Collections.ICollection Keys {get;}
System.Object SyncRoot {get;}
System.Collections.ICollection Values {get;}
Here is the relevant code:
function write-variable {
param(
[Parameter(Mandatory)]
[string]
$variable_name,
[Parameter(Mandatory)]
[AllowNull()][AllowEmptyString()]
$variable_value,
[switch] $showMembers,
[switch] $noNewline,
[switch] $showHeader,
[Parameter(ValueFromRemainingArguments)]
[AllowNull()][AllowEmptyString()]
$FormatingArgs
)
⋮
$var_name = (
$variable_name\[ 1 .. ( $variable_name.length ) ] `
-join ''
)
⋮
$value = (
Get-Variable `
-Name $var_name `
-ValueOnly `
-Scope Script `
-ErrorAction SilentlyContinue
)
if( $NULL -eq $value ) {
$type = $NULL
$string_value = 'Null'
⋮
}
else {
$type = $value.getType().Name
$string_value = 'Collection' # For simplicity
⋮
}
⋮
# The actual code builds a format string and uses
# it to format and output the three variables that
# are seen in this next write statements, but this
# code is enough to show the first line of the
# output shown above.
write-host (
$variable_name + "`t" +
$string_value + "`t" +
'[' + $type + ']'
)
# I want to inject whitespace
# before each line of the 'GM'
# output.
# Here is where I need help:
(
Get-Member `
-InputObject $variable_value `
-MemberType Properties, `
ParameterizedProperty
)
|
( <# Need code here #> )
|
write-host
⋮
}
$v08 = @{ d1 = get-date; d2 = get-date }
write-variable '$v08' $v08 -noNewline
My Goal: Make the grouping of the output lines clearer when this function is called multiple times by indenting the lines (8 lines in this case) that follow the first line of the set (the one containing the variable's name, value, and type) from the output of each call of the function.
I've tried capturing the GM output to a variable and parsing the string, but unlike piping through to the write-host, all of the lines run together, loosing their carriage return and new-line characters, and there doesn't seem to be a consistent punctuation character in the string to parse for, so I can't inject the whitespace (tabs and spaces) to achieve the indentation with. '}' looked hopeful, but not for custom structures' methods nor properties. Besides other information seems to be lost when capturing GM to a string. If the information were not not lost or mangled, then parsing would be fine.
Alternatively, there is piping, which seems to keep the information intact better. I believe that if an object foreach statement were added to the pipe-line where my comment says "Here is where I need help", it could add the whitespace without parsing each line from the GM's string line as it flowed' through, but the "about piping" article and the other article at the MS Learning site didn't give me enough to experiment with or know even if this is the right 'track'.
Please, can someone give me some beginner links, an example, or just let me know if this is the right direction?
Thanks
P.S., I'm using Windows 10, not using VS Code (so no IDE) or any debugger, and I'm very much a newbie at PowerShell.
Update : I'm very close, but ...
Name Value Type
------- ------ -----
$v01 1 [Int32]
$v02 "two" [String]
⋮
$v05 1, 2, 3, 4 [Int32[]]
$v06 one, two, three, four, five
[String[]]
⋮
$v08 Collection
[Hashtable]
Method
Add void Add(Object key, Object value
void IDictionary.Add(Object key, Object value)
⋮
ParameterizedProperty
Item Object Item(Object key) {get;set;}
Property
Count int Count {get;
⋮
$v11 Null
$hash Collection [Hashtable]
Method
Add void Add(Object key, Object value
void IDictionary.Add(Object key, Object value)
Clear void Clear(
void IDictionary.Clear()
⋮
However in the lines for $v08, some of the lines are missing their endings and others aren't. Here are two that are missing:
$v08 Collection [Hashtable]
Method Add void Add(Object key, Object value <-- missing )
Property Count int Count {get; <-- missing }
Here is the preformatted lines:
$v08 Collection [Hashtable]
Method
Add void Add(… key, … value), void IDictionary.Add(… key, … value)
Clear void Clear(), void IDictionary.Clear()
⋮
ParameterizedProperty
Item System.Object Item(… key) {get;set;}
Property
Count int Count {get;}
IsFixedSize bool IsFixedSize {get;}
⋮
I've replaced the parameter types with … to get the lines to not spill over to the next line. And I've removed most of the lines that didn't loose their endings.
I used the regex pattern ( '([)}]),\s*' ) to find the lines that have more than one method or property, specifically those that have a ) or } followed by a , and optional whitespaces, and replace the matches with
'( $crlf + $tab + $tab + $tab + '' )' to push all of the methods or properties after the first one to a new line and over to the right so the methods or properties align with the others in a column.
I used a regex capture group on the ) and } to let put back whichever was found and cut off, but that argument is a PowerShell expression, not regex, and I don't know the auio-variable that contains the $1 matched group need to put the captured ) or } with.
Here is the code:
$this_Definition = `
$this_Definition `
-replace
'([)}]),\s*',
( $crlf + $tab + $tab + $tab + '' )
" {0,-18} {2}" `
-f $_.name,
$MemberType,
$this_Definition
}
|
write-host
Also, what other information is hiding in each line of the piped output from the following Get-Member:
Get-Member -InputObject 'v08' -MemberType ParameterizedProperty, Properties, Methods ?
I've read the help guide on this command, but frankly didn't understand it to use the few options I have. For example. I'd like the property values of the collection. Currently I put in 'Collection' for the type I get now.
Please, someone share how to get the group matches and the options related to variables with me.
Thanks