gpt4 book ai didi

arrays - 为什么我的数组发送到函数后为空

转载 作者:行者123 更新时间:2023-12-02 23:11:01 25 4
gpt4 key购买 nike

我想将几个parameters发送到function中,其中之一是array
在调用function之前,数组包含多个项目,通过函数内的调试器查找时,arrayempty\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
}

最佳答案

$argsautomatic variable,这意味着您无法将args名称用于用户定义的变量。

使用任何其他名称,它将起作用:

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

about_Variables help file:

 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.

关于arrays - 为什么我的数组发送到函数后为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40829786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com