Walter
Walter

Reputation: 1470

How to pass objects in powershell

I can pass one object in powershell, but when I have 2 objects, only 1 is passed, the other is null.

psuedo-code:

function do-something($param1, $param2) {
...
}


do-something($a, $b)

Do I need to do something special to get both args passed to do-something? What I have resorted to is using globals which I think will lead to other issues.

Upvotes: 0

Views: 968

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174465

Although PowerShell allows for a "C#-style" function definition syntax (like in your example), the syntax for invoking functions is a little bit different - in that parameter arguments are separated by whitespace rather than comma, and optionally passed by name.

For what you're trying to do, either of these will work:

Do-Something $a $b
Do-Something -param1 $a -param2 $b
Do-Something -param1:$a -param2:$b

What your current code resolves to is:

Do-Something -param1 ($a, $b)

... which is why you only receive a single argument in the function.

See the about_Functions help file for further details

Upvotes: 2

Related Questions