简体   繁体   English

如何基于动态变量在Powershell中创建和填充数组?

[英]How to create and populate an array in Powershell based on a dynamic variable?

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: 导入具有以下值的csv用户:

ID, Name, Region ID,名称,地区

Create an array based on the Region values that I can then use to populate with ID's and Names with that region, ie. 根据Region值创建一个数组,然后使用该值填充该区域的ID和Name。

Array_SEA Array_SEA

AA_SCOM, Adam Andrews, SEA AA_SCOM,亚当·安德鲁斯,SEA

Array_OAK Array_OAK

BB_SCOM, Bob Barker, OAK BB_SCOM,鲍勃·巴克(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. 对于具有重复区域的记录,这将产生以下错误:New-Variable:名称'@ {Array = Array_SCA}'的变量已存在。

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. 这样的输出不会显示任何错误,但是会一直显示列表的每次迭代的所有记录的已添加字段,因此我认为它实际上并未将每个唯一的BU分配给数组名称。

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: 您也可以使用Group-Object

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM