如何基于动态变量在Powershell中创建和填充数组? [英] How to create and populate an array in Powershell based on a dynamic variable?

查看:61
本文介绍了如何基于动态变量在Powershell中创建和填充数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此苦苦挣扎了几天,但我不确定如何克服它。我需要执行以下操作:



导入具有以下值的用户的CSV:



ID,名称,区域



根据区域值创建一个数组,然后使用该值填充该区域的ID和名称,即



Array_SEA



AA_SCOM,SEA Adam Adams,

Array_OAK



BB_SCOM,Bob Barker,OAK



这是我现在得到的代码:

  $ list2 = ipcsv .\TSE_Contact_List.csv |排序对象BU 

$ arraylist = @()

foreach($ list2中的$ vitem)
{
$ arraylist + =新对象PsObject -Property @ {''Array'= Array_ + $ vitem.bu}
}
foreach($ arraylist中的$ varray)
{
$ arr =新变量-名称$ varray
$ arr.value + = $ varray.array
$ arr
}

对于具有重复区域的记录,这将产生以下错误:
新变量:名称为'@ {Array = Array_SCA}'的变量已经存在。



当它尝试添加值时,也会得到以下内容:
在此对象上找不到属性值;确保它存在并且可设置。



我知道我实际上并没有在第二部分中创建数组,但是我不确定如何传递输出



我已经尝试使用哈希表进行以下操作,并且变得更近了:

  $ list2 = ipcsv .\TSE_Contact_List.csv |排序对象BU 

$ arraylist = @ {}

foreach($ list2中的$ vitem){$ arraylist [$ vitem.bu] = @()}

foreach($ list2中的$ record)
{
$ arraylist [$ vitem.bu] + =($ record.SCOMID,$ record.Name,$ record.BU)
写主机 Array:
$ arraylist [$ vitem.bu]
写主机
}

此输出未显示任何错误,但只是不断显示列表每次迭代的所有记录的已添加字段,因此我认为

解决方案

我喜欢hashtable方法,但是我会对其进行一些微调。 。尝试:

  $ list2 = ipcsv .\TSE_Contact_List.csv |排序对象BU 

$ arraylist = @ {}

foreach($ list2中的$ vitem){
if($ arraylist.ContainsKey($ vitem.BU )){
#Array存在,添加项
$ arraylist [($ vitem.BU)] + = $ vitem
}否则{
#找不到数组,创建它
$ arraylist [($ vitem.BU)] = @($ vitem)
}
}

#TEST:列出数组和条目数
$ arraylist.GetEnumerator()| %{
数组'$($ _。Key)'具有$($ _。Value.Count)个项目
}

您也可以使用 Group-Object ,例如:

  $ list2 = ipcsv .\TSE_Contact_List.csv |组对象BU 

#TEST:列出组(区域)和条目数
$ list2 | %{
区域'$($ _。Name)'具有$(@($ _。Group).Count)个项目
}


I've been struggling with this for a couple of days, and I'm not sure how to conquer it. I need to do the following:

Import a csv of users with the following values:

ID, Name, Region

Create an array based on the Region values that I can then use to populate with ID's and Names with that region, ie.

Array_SEA

AA_SCOM, Adam Andrews, SEA

Array_OAK

BB_SCOM, Bob Barker, OAK

Here's the code I've got right now:

$list2 = ipcsv .\TSE_Contact_List.csv | sort-object BU

$arraylist =@()

foreach ($vitem in $list2)
{
$arraylist += New-Object PsObject -Property @{'Array' = "Array_" + $vitem.bu}
}
foreach ($varray in $arraylist)
{
$arr = new-variable -Name $varray
$arr.value += $varray.array
$arr
}

This produces the following error for records with a duplicate regions: New-Variable: A variable with name '@{Array=Array_SCA}' already exists.

I'm also getting the following when it tries to add values: Property 'value' cannot be found on this object; make sure it exists and is settable.

I get that I'm not actually creating arrays in the second section, but I'm not sure how to pass the output of the variable to an array name without turning the variable declaration into the array name, if that makes sense.

I've tried the following with hash tables, and it gets closer:

$list2 = ipcsv .\TSE_Contact_List.csv | sort-object BU

$arraylist =@{}

foreach ($vitem in $list2){$arraylist[$vitem.bu] = @()}

foreach ($record in $list2)
{
$arraylist[$vitem.bu] += ($record.SCOMID,$record.Name,$record.BU)
Write-host "Array: "
$arraylist[$vitem.bu]
write-host ""
}

The output on this shows no errors, but it just keeps showing the added fields for all of the records for each iteration of the list, so I don't think that it's actually assigning each unique BU to the array name.

解决方案

I like the hashtable-approach, but I would finetune it a little. Try:

$list2 = ipcsv .\TSE_Contact_List.csv | sort-object BU

$arraylist = @{}

foreach ($vitem in $list2){
    if($arraylist.ContainsKey($vitem.BU)) {
        #Array exists, add item
        $arraylist[($vitem.BU)] += $vitem
    } else {
        #Array not found, creating it
        $arraylist[($vitem.BU)] = @($vitem)    
    }
}

#TEST: List arrays and number of entries
$arraylist.GetEnumerator() | % {
    "Array '$($_.Key)' has $($_.Value.Count) items"
}

You could also use Group-Object like:

$list2 = ipcsv .\TSE_Contact_List.csv | Group-Object BU

#TEST: List groups(regions) and number of entries
$list2 | % {
    "Region '$($_.Name)' has $(@($_.Group).Count) items"
}

这篇关于如何基于动态变量在Powershell中创建和填充数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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