如何在PowerShell中向数组动态添加元素? [英] How do I dynamically add elements to arrays in PowerShell?

查看:1281
本文介绍了如何在PowerShell中向数组动态添加元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有太多的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. Bear 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)
}

这篇关于如何在PowerShell中向数组动态添加元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆