将参数作为数组传递给PowerShell函数

Tom*_*eyn 2 powershell reserved automatic-variable

我试图弄清楚如何将多个字符串作为数组传递给Powershell函数。

function ArrayCount([string[]] $args) {
    Write-Host $args.Count
}

ArrayCount "1" "2" "3"
ArrayCount "1","2","3"
ArrayCount @("1","2","3")
Run Code Online (Sandbox Code Playgroud)

版画

2
0
0
Run Code Online (Sandbox Code Playgroud)

如何将具有3个值的数组传递给ArrayCount函数?为什么某些调用的计数为零?

Mar*_*ndl 5

在PowerShell中,$ args是引用未命名参数的自动变量。只需更改您的参数名称:

function ArrayCount([string[]] $myParam) {
    Write-Host $myParam.Count
}
Run Code Online (Sandbox Code Playgroud)

然后您将获得预期的输出

1
3
3
Run Code Online (Sandbox Code Playgroud)