0 Comments

 

When you call a PowerShell function, that expects multiple parameters, with parenthesis, all parameters will be merged in the first parameter, as an array.

The solution is, to call the function with named parameters.

So NOT JoinText($p1, $p2, $p3) but JoinText -p1 $p1 -p2 $p2 -p3 $p3

 

script1.ps1

function JoinText([string]$p1, [string]$p2, [string]$p3) {

return “$p1 $p2 $p3”

}

 

script2-wrong.ps1

$p1 = “some text 1”

$p2 = “some text 2”

$p3 = “some text 3”

$result = JoinText($p1, $p2, $p3)

write-host $result

 

script2-right.ps1

$p1 = “some text 1”

$p2 = “some text 2”

$p3 = “some text 3”

$result = JoinText -p1 $p1 -p2 $p2 -p3 $p3

write-host $result

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts