本文介绍了为什么我的数组发送到函数后为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将几个parameters
发送到function
中,其中一个是array
.在调用function
之前,数组包含多个项目,通过函数内部的调试器查看时,array
为empty\null
:
I want to send several parameters
into function
and one of them is array
.Before call the function
the array is with several items, when looking via the debugger inside the function the array
is empty\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
名称用于用户定义的变量.
$args
is a automatic variable, meaning that you are prevented from using the args
name for a user-defined variable.
使用任何其他名称,它将起作用:
Use any other name and it'll work:
function GetProcessOutput([string]$exeFile,[array]$arguments)
{
# $arguments will work just fine
}
From the 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.
这篇关于为什么我的数组发送到函数后为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!