PowerShell Group Hash Table by Value是一个要获取的数组

时间:2017-05-23 12:57:18

标签: arrays powershell hashtable

从SQL中提取信息,我收集设备名称,AppownerPrimary,Secondary和Tertiary电子邮件地址。可能有0到3个电子邮件地址。对于这个问题,我们基本上可以忽略设备名称。

enter image description here

我使用foreach循环将此信息抛出到哈希表中:

$DeviceList = @{}

$DeviceLists  = (Invoke-Sqlcmd  -Serverinstance $ServerInstance -Database $Database -Query "select devicename, appownerprimaryemail, Secondary, Tertiary from dbo.Table order by AppOwnerPrimaryEmail")

foreach ($item in $devicelists)
{
if($item.appownerprimaryemail -and $item.Secondary -and $item.Tertiary)
{
    $DeviceList += @{$item.DeviceName = "$($item.AppOwnerPrimaryEmail)", "$($item.Secondary)", "$($item.Tertiary)"}
}
elseif ($item.appownerprimaryemail -and $item.Secondary -and !$item.Tertiary)
{
    $DeviceList += @{$item.DeviceName = "$($item.AppOwnerPrimaryEmail)", "$($item.Secondary)"}
}
elseif ($item.appownerprimaryemail -and !$item.Secondary -and !$item.Tertiary)
{
    $DeviceList  += @{$item.DeviceName = "$($item.AppOwnerPrimaryEmail)"}
}
}

哈希表最终看起来像这样:

enter image description here

我只需要获取每次显示唯一电子邮件分组的计数。当我开始编写脚本时,只有一封电子邮件,我在SQL查询中按电子邮件排序,所以我能够使用group-object:

    [array]$UniqueEmails = $DeviceList.Values | Group-Object

现在可能有0-3,组对象根本不起作用。它似乎是为每个设备将所有电子邮件分组在一起。我知道group-object不喜欢hashtables,但是我不确定为什么这样,你知道为什么会这样,我想要一个解释。但是,这篇文章的重点是我只需要计算每个唯一值字符串。

使用这个例子,我希望这个输出

enter image description here

您可以通过运行以下内容来复制问题:

$Devicelist = @{"Device1" = "Sally.Johnson@Domain.com"; "Device7" = "Sally.Johnson@Domain.com"; "Device8" = "Sally.Johnson@Domain.com"; "Device2" = "Bob.Davis@domain.com", "Jane.Doe@domain.com"; "Device3" = "Bob.Davis@domain.com", "Jane.Doe@domain.com"; "Device4" = "Jane.Doe@domain.com", "Sally.Johnson@domain.com", "Bob.Davis@domain.com"; "Device5" = "Jane.Doe@domain.com", "Sally.Johnson@domain.com", "Bob.Davis@domain.com"; "Device6" = "Jane.Doe@domain.com", "Sally.Johnson@domain.com", "Bob.Davis@domain.com"}
[array]$UniqueEmails = $DeviceList.values | Group-Object

我认为我需要枚举并遍历散列表值的数组中的每个项目,但同时我需要将它们视为一个组,所以我不知所措。非常感谢任何帮助。

如果我不清楚,请告诉我,我会尽力解释。

1 个答案:

答案 0 :(得分:1)

$devicelist = @{
    "Device1" = "Sally.Johnson@Domain.com";
    "Device7" = "Sally.Johnson@Domain.com";
    "Device8" = "Sally.Johnson@Domain.com";
    "Device2" = "Bob.Davis@domain.com", "Jane.Doe@domain.com";
    "Device3" = "Bob.Davis@domain.com", "Jane.Doe@domain.com";
    "Device4" = "Jane.Doe@domain.com", "Sally.Johnson@domain.com", "Bob.Davis@domain.com";
    "Device5" = "Jane.Doe@domain.com", "Sally.Johnson@domain.com", "Bob.Davis@domain.com";
    "Device6" = "Jane.Doe@domain.com", "Sally.Johnson@domain.com", "Bob.Davis@domain.com"
}

$uniqueEmails = @{}

# Get all the values in the list, sort them
# and for each, use the value as the key in the unique emails list
# then for that key add 1 every time the key repeats
$devicelist.Values | Sort | % { $uniqueEmails["$_"] += 1 }

Write-Host ($uniqueEmails | Out-String)

# Outputs
Name                           Value
----                           ---- -
Sally.Johnson@Domain.com       3
Jane.Doe@domain.com Sally.J... 3
Bob.Davis@domain.com Jane.D... 2

<强>更新

我在Posh v1,Posh v2和Posh v3上测试了它,它在2012 Server R2上运行正常。问题必须与您的服务器有关。运行$ PSVersionTable.PSVersion并查看您正在使用的版本。

您可以使用以下命令启动任何较旧的Posh版本:powershell -version 1 | 2 | 3.