0

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.

0

1 Answer 1

2

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

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

2 Comments

Thanks, that was it. Being new to powershell, the syntax is a little bit different than what I'm used to. This was very useful. I'll have another go at about Functions, I thought I could call it the way I was used to doing.
Yeah, the PowerShell learning curve is a bit weird, but practice makes a master :) Be sure to check out the about_Functions_Advanced* topics as well - the syntax you're currently using is great for many general-purpose functions, but advanced functions allow a greater deal of control over input binding and the likes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.