I have a weird PowerShell problem. I am trying to pass into a class method multiple parameters, but it fails. I was able to pass in multiple parameters to a global function instead and that worked. Trying to pass in a variable amount of parameters to a class method instead fails!
Here is my code:
class TestParams { [int] $dummyVar TestParams() { $this.dummyVar = 0 } [void]myMethod() { for ($index = 0; $index -lt $args.Count; $index++) { Write-Host $args[$index] } } } function testFunc() { for ($index = 0; $index -lt $args.Count; $index++) { Write-Host $args[$index] } } testFunc '1' '2' '3' # works $myTestObj = New-Object TestParams $myTestObj.myMethod "A" "B" "C" # fails As you can see from running my code it gives error messages such as:
At C:\*****\testParams.ps1:25 char:21 + $myTestObj.myMethod "A" "B" "C" + ~~~ Unexpected token '"A"' in expression or statement.
I do not know what is causing this error! Can you guys help me debug this?
$obj.MyMethod('A', 'B', 'C')with methods, andInvoke-Function 'A' 'B' 'C'with (advanced) functions.