如何动态地添加元素在PowerShell来阵列 [英] how to dynamically add elements to arrays in powershell

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

问题描述

我没有太多的PowerShell的经验,但,我试图教我,因为我沿着

I don't have much powershell experience yet and am trying to teach myself as i go along

我试图使概念code的一些证明一个更大的项目,这里的主要目标是太动态创建和使用功能的添加元素的数组。

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.

这是我的code:

$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

推荐答案

问题是范围之一;您addToArray函数中将该行更改为这样的:

The problem is one of scope; inside your addToArray function change the line to this:

$script:testArray += $Item1

...存储到你所期望的数组变量。

...to store into the array variable you are expecting.

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

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