mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Ensure -PipelineVariable is set for all output from script cmdlets (#12766)
This commit is contained in:
@@ -3751,6 +3751,17 @@ namespace System.Management.Automation
|
||||
|
||||
if (this.PipelineVariable != null)
|
||||
{
|
||||
// _state can be null if the current script block is dynamicparam, etc.
|
||||
if (_state != null)
|
||||
{
|
||||
// Create the pipeline variable
|
||||
_state.PSVariable.Set(_pipelineVarReference);
|
||||
|
||||
// Get the reference again in case we re-used one from the
|
||||
// same scope.
|
||||
_pipelineVarReference = _state.PSVariable.Get(this.PipelineVariable);
|
||||
}
|
||||
|
||||
this.OutputPipe.SetPipelineVariable(_pipelineVarReference);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,6 +256,62 @@ Describe "Parameter Binding Tests" -Tags "CI" {
|
||||
DynamicParamTest -PipelineVariable bar | ForEach-Object { $bar } | Should -Be "hi"
|
||||
}
|
||||
|
||||
Context "PipelineVariable Behaviour" {
|
||||
|
||||
BeforeAll {
|
||||
function Write-PipelineVariable {
|
||||
[CmdletBinding()]
|
||||
[OutputType([int])]
|
||||
param(
|
||||
[Parameter(ValueFromPipeline)]
|
||||
$a
|
||||
)
|
||||
begin { 1 }
|
||||
process { 2 }
|
||||
end { 3 }
|
||||
}
|
||||
|
||||
$testScripts = @(
|
||||
@{
|
||||
CmdletType = 'Script Cmdlet'
|
||||
Script = {
|
||||
1..3 |
|
||||
Write-PipelineVariable -PipelineVariable pipe |
|
||||
Select-Object -Property @(
|
||||
@{ Name = "PipelineVariableSet"; Expression = { $null -ne $pipe ? $true : $false } }
|
||||
@{ Name = "PipelineVariable"; Expression = { $pipe } }
|
||||
)
|
||||
}
|
||||
}
|
||||
@{
|
||||
CmdletType = 'Compiled Cmdlet'
|
||||
Script = {
|
||||
1..3 |
|
||||
Write-PipelineVariable |
|
||||
ForEach-Object { $_ } -PipelineVariable pipe |
|
||||
Select-Object -Property @(
|
||||
@{ Name = "PipelineVariableSet"; Expression = { $null -ne $pipe ? $true : $false } }
|
||||
@{ Name = "PipelineVariable"; Expression = { $pipe } }
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
AfterAll {
|
||||
Remove-Item -Path 'function:Write-PipelineVariable'
|
||||
}
|
||||
|
||||
It 'should set the pipeline variable every time for a <CmdletType>' -TestCases $testScripts {
|
||||
param($Script, $CmdletType)
|
||||
|
||||
$result = & $Script
|
||||
$result.Count | Should -Be 5
|
||||
$result.PipelineVariableSet | Should -Not -Contain $false
|
||||
$result.PipelineVariable | Should -Be 1, 2, 2, 2, 3
|
||||
}
|
||||
}
|
||||
|
||||
Context "Use automatic variables as default value for parameters" {
|
||||
BeforeAll {
|
||||
## Explicit use of 'CmdletBinding' make it a script cmdlet
|
||||
|
||||
Reference in New Issue
Block a user