1

I'm having trouble passing two parameters via pipeline to a function.

function Test{
[cmdletbinding()]
param(
[parameter(ValueFromPipeline=$true, Mandatory=$true,Position=0)]
[string]$jeden,
[parameter(ValueFromPipeline=$true, Mandatory=$true,Position=1)]
[string]$dwa
)
Process{write-host "$jeden PLUS $dwa"}
}

"one", "two"|Test

What I expected as an outcome was

one PLUS two

but what I got was

one PLUS one
two PLUS two

I'm obviously doing something wrong, since both parameters get used twice. Please advise.

2 Answers 2

2

I got it to work by creating pscustomobject and piping it to function, where ValueFromPipelineByPropertyName property is set to true for both parameters.

function Test{
[cmdletbinding()]
param(
[parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true,Position=0)]
[string]$jeden,
[parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true,Position=1)]
[string]$dwa
)
Process{write-host "$jeden PLUS $dwa"}
}

$Params = [pscustomobject]@{
    jeden = “Hello”
    dwa = “There”
}

$Params |Test

OUTPUT:

Hello PLUS There

Variable assigning can be skipped and [pscustomobject] can be piped directly.

Sign up to request clarification or add additional context in comments.

Comments

1

Have u tried to pass both strings as single object? Seems its ur pipeline is treating dem as 2 obj...

@("one", "two") | Test

EDIT. Try to define test in order to accept array:

function Test {
    [cmdletbinding()]
    param(
        [parameter(ValueFromPipeline=$true, Mandatory=$true,Position=0)]
        [string[]]$strings
    )
    Process {
        write-host "$($strings[0]) PLUS $($strings[1])"
    }
}

3 Comments

I tried that and the result is the same
maybe you can try to define your Test function to accept an array and then use the pipeline to pass the array of strings. Check my edited comment and try it
With the edited code the outcome is different but not what I need one PLUS two PLUS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.