Arrays 将数组项传递给PowerShell中的函数

Arrays 将数组项传递给PowerShell中的函数,arrays,function,powershell,Arrays,Function,Powershell,我已经阅读了许多关于PowerShell函数和向其传递参数的文章,但我还没有找到一个解决方案,说明如何将特定数组项传递给函数而不是整个数组。 这就是我的代码的外观: $log = "C:\Temp\test.txt" $test = "asdf" $arrtest = @("one", "two", "three") Function Write-Log($message) { Write-Host $message $message | Out-File $log -Appe

我已经阅读了许多关于PowerShell函数和向其传递参数的文章,但我还没有找到一个解决方案,说明如何将特定数组项传递给函数而不是整个数组。
这就是我的代码的外观:

$log = "C:\Temp\test.txt"
$test = "asdf"
$arrtest = @("one", "two", "three")

Function Write-Log($message)
{
    Write-Host $message
    $message | Out-File $log -Append
}
现在我想将数组的单个项传递给Write Log函数,如下所示:

Write-Log "first arr item: $arrtest[0]"
Write-Log "second arr item: $arrtest[1]"
Write-Log "third arr item: $arrtest[2]"
但在命令行中,我总是将完整数组加上[number]作为字符串:

first arr item: one two three[0]
second arr item: one two three[1]
third arr item: one two three[2]
我想问题出在我的语法上,有人能给我指出正确的方向吗


非常感谢

这将实现以下目的:

Write Log“第一个arr项:$($arrtest[0])”


在您的尝试中,您将传递整个数组,因为PowerShell将
$arrtest
解释为变量,将
[0]
解释为字符串。

非常感谢您提供的快速解决方案!