r/PowerShell • u/SymfonyWonderer2 • 5h ago
Question How to inject a tab before each output line of Get-Member?
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 -noNewlineMy 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.