I have a script that I’m trying to add pipeline functionality to. I’m seeing the strange behavior, though, where the script seems to only be run against the final object in the pipeline. For example
param(
[parameter(ValueFromPipeline=$true)]
[string]$pipe
)
foreach ($n in $pipe) {
Write-Host "Read in " $n
}
Dead simple, no? I then run 1..10 | .\test.ps1 and it only outputs the one line Read in 10. Adding to the complexity, the actual script I want to use this in has more to the parameters:
[CmdletBinding(DefaultParameterSetName="Alias")]
param (
[parameter(Position=0,ParameterSetName="Alias")]
[string]$Alias,
[parameter(ParameterSetName="File")]
[ValidateNotNullOrEmpty()]
[string]$File
<and so on>
)
You need to wrap the main body of the script with process{}, this will then allow you to process each item on the pipeline. As process will be called for each item you can even do away with the for loop.
So your script will read as follows:
You can read about input processing here: Function Input Processing Methods