Arrays 为什么我的数组在发送到函数后为空

Arrays 为什么我的数组在发送到函数后为空,arrays,powershell,arguments,Arrays,Powershell,Arguments,我想将几个参数发送到函数中,其中一个是数组。 调用函数之前数组包含多个项目,通过调试器查看函数内部时,数组为空\null: $arr = New-Object System.Collections.ArrayList $arr.Add("test1") GetProcessOutput -exeFile "c:\file.exe" -args $arr function GetProcessOutput($exeFile, $args) { # here my $args is em

我想将几个
参数
发送到
函数
中,其中一个是
数组
。 调用
函数之前
数组包含多个项目,通过调试器查看函数内部时,
数组
空\null

$arr = New-Object System.Collections.ArrayList 
$arr.Add("test1")
GetProcessOutput -exeFile "c:\file.exe" -args $arr

function GetProcessOutput($exeFile, $args)
{
    # here my $args is empty -> children could not be evaluated
}
$args
是一个,这意味着您不能为用户定义的变量使用
args
名称

使用任何其他名称都可以:

function GetProcessOutput([string]$exeFile,[array]$arguments)
{
    # $arguments will work just fine
}

从:


$args
->
${其他与$args自动变量不冲突的变量}
您还应该将函数定义移到函数调用上方,以提高脚本可读性。做得很好。不幸的是,对于
$args
而言,“阻止使用”意味着:“您可以使用此名称自由声明参数,但它将被忽略”,这可能是一个bug。相比之下,例如,尝试命名参数
$PSHOME
,会更明智地导致错误
无法覆盖变量PSHOME,因为它是只读或常量。
There are several different types of variables in Windows PowerShell.
-- User-created variables: User-created variables are created and 
   maintained by the user. By default, the variables that you create at
   the Windows PowerShell command line exist only while the Windows 
   PowerShell window is open, and they are lost when you close the window.
   To save a variable, add it to your Windows PowerShell profile. You can 
   also create variables in scripts with global, script, or local scope.    

-- Automatic variables: Automatic variables store the state of 
   Windows PowerShell. These variables are created by Windows PowerShell, 
   and Windows PowerShell changes their values as required to maintain 
   their accuracy. Users cannot change the value of these variables.
   For example, the $PSHome variable stores the path to the Windows 
   PowerShell installation directory.