如何在powershell中动态添加元素到数组
问题描述:
我没有太多的权力经验,我正在尝试自我教训,因为我一直在
I don't have much powershell experience yet and am trying to teach myself as i go along
我正在尝试一些概念代码证明更大的项目,这里的主要目标是动态创建并使用函数向数组添加元素。
i'm trying to make some proof of concept code for a bigger project, the main goal here is too dynamically create and add elements to an array using a function.
这是我的代码:
$testArray = @()
function addToArray($Item1)
{
$testArray += $Item1
"###"
}
$tempArray = "123", "321", "453"
$foldertest = "testFolder"
foreach($item in $tempArray)
{
addToArray $item
}
"###"
每当函数完成数组变为空时,我的大部分程序设计经验来自于java,php,一些C和C ++,只是为了命名几个,如果我在php(调整语言语法当然)这样做会很好的。 >
every time the function finishes the array becomes empty. bare in mind most of my programming experience comes from java, php, some C and C++ just to name a few, if i did this in php (adjusting the language syntax of course) this would have worked fine
答
$testArray = [System.Collections.ArrayList]@()
$tempArray = "123", "321", "453"
foreach($item in $tempArray)
{
$arrayID = $testArray.Add($item)
}