Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

problem with the powershell pipline parameter process

$
0
0

Hi,


I'm confused with the pipeline processing of object-property-parameter association.

Here my Cmdlet example

 

Function test-psboundbug{
	[CmdletBinding(DefaultParameterSetName='Parameter')]
 	param(
 	    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)]
 	    [string]$Param1,
 	    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)]
 	    [string]$Param2,
 	    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)]
 	    [string]$Param3,
 	    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)]
 	    [string]$Param4,
 	    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)]
 	    [string]$Param5
    )    
    Begin{}
    Process{
        Write-Host " ====== BEGIN - PROGRESS =============="
        $PSBoundParameters
        Write-Host " ====== BEGIN - PROGRESS =============="
    }
    End{}
}
$testParams=@()
$testParams+=@{Param1="Param1-1";Param2="Param2-1"}
$testParams+=@{Param1="Param1-2";Param2="Param2-2";Param3="Param3-2"}
$testParams+=@{Param1="Param1-3";Param2="Param2-3";Param4="Param4-3"}
$testParams+=@{Param1="Param1-4";Param2="Param2-4"}
$testParams+=@{Param1="Param1-5";Param2="Param2-5"}
$testParams|%{New-Object psobject -Property $_}|test-psboundbug

when i send a array of objects through a pipline, i thoucht in every loop in the ''PROCESS''-Scriptblock the parameter accociation from the object properties is caculate exact on the behavior of the single object, which is active in processing.
But when the Property-Names are alterate in pipeline (see the example above), powershell is not resetting the $PSBoundParameters variable,but rather the $PSBoundParameters contains old values from the parent object in the pipeline loop, up to the end.

Here the Output of the example above:

 ====== BEGIN - PROGRESS ==============

Key                                     Value 
---                                     -----
Param1                                  Param1-1
Param2                                  Param2-1
 ====== BEGIN - PROGRESS ==============
 ====== BEGIN - PROGRESS ==============
Param1                                  Param1-2
Param2                                  Param2-2
Param3                                  Param3-2
 ====== BEGIN - PROGRESS ==============
 ====== BEGIN - PROGRESS ==============
Param1                                  Param1-3
Param2                                  Param2-3
Param3                                  Param3-2
Param4                                  Param4-3
 ====== BEGIN - PROGRESS ==============
 ====== BEGIN - PROGRESS ==============
Param1                                  Param1-4
Param2                                  Param2-4
Param3                                  Param3-2
Param4                                  Param4-3
 ====== BEGIN - PROGRESS ==============
 ====== BEGIN - PROGRESS ==============
Param1                                  Param1-5
Param2                                  Param2-5
Param3                                  Param3-2
Param4                                  Param4-3
 ====== BEGIN - PROGRESS ==============

The $PSBoundParameters contains the value of Param3 und Param4 up to end of whole process. I can manually Reset $PSBoundParameter.Clean() on the End of the PROCESS-Block and all worked as expected.

My Question is , is this a bug or a feature.

Jens Müller



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>