PowerShell Passing Multiple Parameters to a Class Method Fails

后端 未结 2 759
孤独总比滥情好
孤独总比滥情好 2021-01-16 08:10

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

2条回答
  •  执笔经年
    2021-01-16 08:40

    You need to declare the parameters in the method definition:

    [void]
    myMethod( [array]$arguments ) {
        for ($index = 0; $index -lt $arguments.Count; $index++) {
            Write-Host $arguments[$index]
        }
    }
    

    Note that I intentionally changed the automatic variable name args to something else, otherwise it won't work.

    In order to call the method, use the following syntax:

    $myTestObj.myMethod(@('A', 'B', 'C'))
    # or
    $argument = 'A', 'B', 'C'
    $myTestObj.myMethod($argument)
    

提交回复
热议问题