简体   繁体   English

PowerShell Group哈希表(按值),该值是要获取计数的数组

[英]PowerShell Group Hash Table by Value Which is an Array to Get Count

Pulling information from SQL, I gather a device name, AppownerPrimary, Secondary, and Tertiary email address. 从SQL中提取信息,我收集了设备名称,AppownerPrimary,次要和第三级电子邮件地址。 There could be 0 to 3 email addresses. 可能有0到3个电子邮件地址。 For this problem, we can essentially disregard the device name. 对于此问题,我们实际上可以忽略设备名称。

在此处输入图片说明

I throw this information into a hash table using a foreach loop: 我使用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)"}
}
}

The hash table ends up looking like this: 哈希表最终看起来像这样:

在此处输入图片说明

I simply need to get the count of each time a unique email grouping appears. 我只需要获取每次出现唯一电子邮件分组的次数即可。 When I started writing the script, there was only one email and I am sorting by email in the SQL query, so I was able to use group-object: 当我开始编写脚本时,只有一封电子邮件,并且在SQL查询中按电子邮件进行排序,因此我可以使用组对象:

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

Now that there could be 0-3, group object is not working, at all. 现在可能是0-3,组对象根本不起作用。 It seems to be grouping all of the emails together for every device. 似乎是将每个设备的所有电子邮件分组在一起。 I know that group-object does not like hashtables, but I'm not certain on why so f you know why this is happening, I would love an explanation. 我知道组对象不喜欢哈希表,但是我不确定为什么要知道为什么会发生这种情况,所以我想解释一下。 However, the point of this post is that I simply need a count of each unique value string. 但是,本文的重点是我只需要对每个唯一值字符串进行计数。

Using this example, I would expect this output 使用这个例子,我期望这个输出

在此处输入图片说明

You can replicate the issue by running this: 您可以通过运行以下命令来复制问题:

$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

I think that I need to enumerate and loop through every item in the array of the values of the hash table, but at the same time I need to treat them as a group so I'm at a loss. 我认为我需要枚举并遍历哈希表的值数组中的每个项目,但是同时我需要将它们视为一个组,所以我很茫然。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

If I'm not clear, please let me know and I will do my best to explain better. 如果我不清楚,请让我知道,我会尽力更好地解释。

$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

Update 更新资料

I tested this on Posh v1, Posh v2 and Posh v3, and it works fine on 2012 Server R2 as well. 我在Posh v1,Posh v2和Posh v3上进行了测试,并且在2012 Server R2上也能正常工作。 The issues must be with your server. 问题必须出在您的服务器上。 Run $PSVersionTable.PSVersion and see what version you are using. 运行$ PSVersionTable.PSVersion并查看使用的版本。

You can start any older Posh version with: powershell -version 1|2|3. 您可以使用以下命令启动任何较旧的Posh版本:powershell -version 1 | 2 | 3。

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

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