Skip to content

Commit c0cbd62

Browse files
fix: Write-FormatTableView - Fixing sticky piped variables ( Fixes #235 )
1 parent 128b80c commit c0cbd62

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

Commands/Format.PS1XML/Write-FormatTableView.ps1

+22-5
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function Write-FormatTableView
104104

105105
# If provided, will colorize all rows in a table, according to the script block.
106106
# If the script block returns a value, it will be treated either as an ANSI escape sequence or up to two hexadecimal colors
107-
[Parameter(ValueFromPipelineByPropertyName=$true)]
107+
[Parameter(ValueFromPipelineByPropertyName)]
108108
[Alias('ColourRow')]
109109
[ScriptBlock]$ColorRow,
110110

@@ -136,20 +136,21 @@ function Write-FormatTableView
136136

137137
# If provided, the table view will only be used if the the typename includes this value.
138138
# This is distinct from the overall typename, and can be used to have different table views for different inherited objects.
139-
[Parameter(ValueFromPipelineByPropertyName=$true)]
139+
[Parameter(ValueFromPipelineByPropertyName)]
140140
[string]
141141
$ViewTypeName,
142142

143143
# If provided, the table view will only be used if the the typename is in a SelectionSet.
144144
# This is distinct from the overall typename, and can be used to have different table views for different inherited objects.
145-
[Parameter(ValueFromPipelineByPropertyName=$true)]
145+
[Parameter(ValueFromPipelineByPropertyName)]
146146
[string]
147147
$ViewSelectionSet,
148148

149149
# If provided, will selectively display items.
150-
[Parameter(ValueFromPipelineByPropertyName=$true)]
150+
[Parameter(ValueFromPipelineByPropertyName)]
151151
[ScriptBlock]
152-
$ViewCondition)
152+
$ViewCondition
153+
)
153154

154155
begin {
155156
$rowEntries = @()
@@ -158,9 +159,25 @@ function Write-FormatTableView
158159
if ($_ -is [scriptblock]) { "`$(`$Script:_LastCellStyle = `$(`$__ = `$_;. {$($_)};`$_ = `$__);`$Script:_LastCellStyle)"}
159160
else { "`$(`$Script:_LastCellStyle ='$($_)';`$Script:_LastCellStyle)" }
160161
}
162+
163+
$myParameterNames = @(($MyInvocation.MyCommand -as [Management.Automation.CommandMetadata]).Keys) -as [string[]]
161164
}
162165

163166
process {
167+
# ValueFromPipelineByPropertyName is great, but it is "sticky".
168+
# Parameters that have been bound aren't "unbound" until a new value is provided.
169+
# This means that if a parameter is not provided, it will keep the last value.
170+
# And that's what we call an unexpected side effect.
171+
# In order to avoid this, we walk over our list of parameter names
172+
foreach ($parameterName in $myParameterNames) {
173+
# if they are not in the bound parameters
174+
if (-not $PSBoundParameters.ContainsKey($parameterName)) {
175+
# we nullify them
176+
try { $ExecutionContext.SessionState.PSVariable.Set($parameterName, $null) }
177+
# and if we can't, we write a verbose message.
178+
catch { Write-Verbose "Could not nullify '$parameterName': $_ " }
179+
}
180+
}
164181
$tableHeader = ''
165182
$rowColumns =
166183
@(for ($i =0; $i -lt $property.Count; $i++) {

0 commit comments

Comments
 (0)