如何在powershell中动态添加元素到数组 [英] how to dynamically add elements to arrays in powershell

查看:519
本文介绍了如何在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)
}

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

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