15

I sense that I am doing something silly, but here is the issue:

Function getPropertyOfFile($a, $b, $c)
{
    $a.GetDetailsOf($b, $c)
}

If I pass $a, $b, $c variables that are appropriate to the function, it fails saying that

"Method invocation failed because [System.Object[]] doesn't contain a method named 'GetDetailsOf'."

However, if I directly replace $a, $b, $c with the arguments that I was passing, and then try to run that, it works fine.

What the heck is going on?

Note: I am using powershell ISE, and am inputting the function to powershell by copy/pasting it into the console. I have also been working under the assumption that if I input a new function with the same name, it would overwrite. Is there a better way to just have PS read from the .ps1?

Edit: I am trying to wrap the answer to this question into functions.

Edit 2:

Function getPropertyOfFile $a $b $c
{
    $a.GetDetailsOf($b, $c)
}

Gives an Missing function body in function declaration. At line:1 char:28 error.

3
  • 2
    You need to show the code you are passing to the function... But try: $a[0].GetDetailsOf($b, $c) Commented Feb 23, 2012 at 20:47
  • I am passing the $shellfolder, $shellfile, variables that were defined in the post I linked to, and property is just a number Commented Feb 23, 2012 at 20:50
  • No you had the function getPropertyOfFile($a, $b, $c) part right... It's just when you call it you want to use spaces between the parameters, not commas because the comma is the PowerShell array operator. Commented Feb 23, 2012 at 20:56

2 Answers 2

23

Functions in PowerShell are called similar to cmdlets, so you don't need to separate arguments with commas.

Your invocation likely looks like this:

getPropertyOfFile($foo, $bar, $baz)

which results in $a having the value $foo, $bar, $baz (an array) while $b and $c are $null.

You need to call it like this:

getPropertyOfFile $foo $bar $baz

which, as noted, is identical to how you call cmdlets. You could even do

getPropertyOfFile -a $foo -c $baz -b $bar

at which point you probably notice that your function arguments aren't named very well ;-)

EDIT: As noted before your declaration of the function is fine. The problem is in the code you didn't post but is easily inferrable for people with PowerShell experience. Namely, the invocation of your function.

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

3 Comments

If I remove the commas it says: Missing ')' in function parameter list.
This is probably the culprit... unless $a is being created as an array somewhere.
Invocation, not declaration. Your declaration is fine (apart from the weird argument names).
3

You need to separate your arguments when calling the function with spaces rather than commas i.e.

getPropertyOfFile $arg1 $arg2 $arg3

instead of

getPropertyOfFile $arg1, $arg2, $arg3

The second form will pass a single array containing $arg1, $arg2 and $arg3 as the parameter $a

2 Comments

I get: Missing function body in function declaration. At line:1 char:28
@soandos - I mean when you call the function, not in the declaration

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.