Powershell argument passed to function doesn't seem to work

I feel like doing something stupid, but here is the problem:

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

If I pass the variables $ a, $ b, $ c corresponding to this function, this does not mean that

"Method error because [System.Object []] does not contain a method named" GetDetailsOf ".

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

What's happening?

Note. I am using powershell ISE and injecting a function into powershell by copying / pasting it into the console. I also worked on the assumption that if I introduce a new function with the same name, it will be overwritten. Is there a better way to just read PS from .ps1?

Edit: I am trying to transfer the answer to this question in a function.

Edit 2:

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

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

+7
source share
2 answers

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

Your call might look like this:

 getPropertyOfFile($foo, $bar, $baz) 

resulting in $a having the value $foo, $bar, $baz (array), and $b and $c $null .

You need to call it like this:

 getPropertyOfFile $foo $bar $baz 

which, as noted, is identical to how you invoke the cmdlets. You can even do

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

at this point, you will probably notice that your function arguments are not named very well; -)

EDIT: As noted before your ad function is excellent . The problem is code that you haven’t published but is easily displayed for people with PowerShell experience. Namely, calling your function.

+13
source

You need to separate your arguments when you call the function with spaces, not commas i.e.

 getPropertyOfFile $arg1 $arg2 $arg3 

instead

 getPropertyOfFile $arg1, $arg2, $arg3 

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

+3
source

All Articles