How to use pipeline pass the parameter

i try to write a script can store the prompt i frequently use as a batch file
and i want to use the pipeline to pass the parameter

'$Variable= Import-Csv C:\Users\Administrator\demo.csv
>>
>> $attributelist= $Variable|Get-Member|Where-Object -Property MemberType -Like "*noteproperty*"|Select-Object -Property Name
>>
>>
>> class hash {
>> [string]$name
>>
>>     hash ([string]$name ){
>>
>>         $this.name = $name
>>     }
>>
>>
>> }
>>
>> $attributelist
>>
>> [hash]::new(
>>
>> @{
>>
>>     name = "1"
>> }
>> )
>> '|store.ps1

and i try the regular way

[CmdletBinding()]
param (
    [Parameter()]
    [string]
    $ParameterName
)

Write-Host $ParameterName
'for ( ($loop=1);$loop -eq  1){
 & "D:\Program Files\streamlink\bin\streamlink.exe" "https://www.twitch.tv/billibits" best
}'|& "C:\Program Files\test\store.ps1"


The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that tak
e pipeline input.
At line:3 char:4
+ }'|& "C:\Program Files\test\store.ps1"
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (for ( ($loop=1)...ibits" best
}:String) [demo(5).ps1], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,demo(5).ps1

Fundamentally, you modify the Parameter() decorator to tell PowerShell that the parameter accepts pipeline input:

function Test-Pipeline {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $True)]
        [String]
        $MyParam
    )

    $MyParam
}

'Hello' | Test-Pipeline

It can quickly get more complicated than that though :slight_smile:

1 Like

Yes, especially considering your example won’t work. You need to either user a process block or use the $input automatic variable

Currently this is the output

'a','b','c' | Test-Pipeline

c

With a process block

function Test-Pipeline {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $True)]
        [String]
        $MyParam
    )

    process{
        $MyParam
    }
}

'a','b','c' | Test-Pipeline

a
b
c

With $input automatic variable

function Test-Pipeline {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $True)]
        [String]
        $MyParam
    )

    $input
}

'a','b','c' | Test-Pipeline

a
b
c

As you can see, on the last example the parameter isn’t even used/needed. Definitely recommend using the process block.

2 Likes