如何遍历 Group-Object 的结果
How to loop through results of Group-Object
我喜欢 Group-Object
的功能,但我不知道如何访问它 returns。
我想将唯一的 URL 列表写入文件(仅此而已),首先按最高计数排序。甚至不确定我是否需要 -AsHashTable
或 -AsString
,尝试使用和不使用。
$urls = @()
$urls += "http://example.com/test.aspx?a1"
$urls += "http://example.com/test.aspx?b2"
$urls += "http://example.com/test.aspx?c3"
$urls += "http://example.com/test.aspx?a1"
$urls += "http://example.com/test.aspx?b2"
$urls += "http://example.com/test.aspx?a1"
$urls += "http://example.com/test.aspx?a1"
Write-Host "Group & Count"
$urls | Group-Object
#the above is perfect, I just access to the individual fields there
Write-Host "`n`nSummary:"
$summaryUrls = Group-Object -InputObject $urls #| Select Name, Count | Format-Table -Auto
$summaryUrls
Write-Host "`n`nLoop:"
foreach ($summaryUrl in $summaryUrls) {
Write-Host "Name=$($summaryUrl.Name) Count=$($summaryUrl.Count)"
#could build new array here to write to file but loop isn't doing anything
}
输出:
Group & Count
Count Name Group
----- ---- -----
4 http://example.com/tes... {http://example.com/test.aspx?a1, http://example.com/test.aspx?a1, http://example.com/test.aspx?a1, http://example.com/test.aspx?a1}
2 http://example.com/tes... {http://example.com/test.aspx?b2, http://example.com/test.aspx?b2}
1 http://example.com/tes... {http://example.com/test.aspx?c3}
Summary:
1 http://example.com/tes... {http://example.com/test.aspx?a1 http://example.com/test.aspx?b2 http://example.com/test.aspx?c3 http://example.com/test.aspx?a1 http://e...
Loop:
http://example.com/test.aspx?a1 http://example.com/test.aspx?b2 http://example.com/test.aspx?c3 http://example.com/test.aspx?a1 http://example.com/test.aspx?b2 http://exampl
e.com/test.aspx?a1 http://example.com/test.aspx?a1 1
PowerShell 版本:5.1.17763.592
-InputObject
Specifies the objects to group. Enter a variable that contains the objects, or type a command or expression that gets the objects.
When you use the InputObject parameter to submit a collection of objects to Group-Object
, Group-Object
receives one object that represents the collection. As a result, it creates a single group with that object as its member.
To group the objects in a collection, pipe the objects to Group-Object
.
因此您需要在作业中使用带有管道的版本:
$summaryUrls = $urls | Group-Object
我喜欢 Group-Object
的功能,但我不知道如何访问它 returns。
我想将唯一的 URL 列表写入文件(仅此而已),首先按最高计数排序。甚至不确定我是否需要 -AsHashTable
或 -AsString
,尝试使用和不使用。
$urls = @()
$urls += "http://example.com/test.aspx?a1"
$urls += "http://example.com/test.aspx?b2"
$urls += "http://example.com/test.aspx?c3"
$urls += "http://example.com/test.aspx?a1"
$urls += "http://example.com/test.aspx?b2"
$urls += "http://example.com/test.aspx?a1"
$urls += "http://example.com/test.aspx?a1"
Write-Host "Group & Count"
$urls | Group-Object
#the above is perfect, I just access to the individual fields there
Write-Host "`n`nSummary:"
$summaryUrls = Group-Object -InputObject $urls #| Select Name, Count | Format-Table -Auto
$summaryUrls
Write-Host "`n`nLoop:"
foreach ($summaryUrl in $summaryUrls) {
Write-Host "Name=$($summaryUrl.Name) Count=$($summaryUrl.Count)"
#could build new array here to write to file but loop isn't doing anything
}
输出:
Group & Count Count Name Group ----- ---- ----- 4 http://example.com/tes... {http://example.com/test.aspx?a1, http://example.com/test.aspx?a1, http://example.com/test.aspx?a1, http://example.com/test.aspx?a1} 2 http://example.com/tes... {http://example.com/test.aspx?b2, http://example.com/test.aspx?b2} 1 http://example.com/tes... {http://example.com/test.aspx?c3} Summary: 1 http://example.com/tes... {http://example.com/test.aspx?a1 http://example.com/test.aspx?b2 http://example.com/test.aspx?c3 http://example.com/test.aspx?a1 http://e... Loop: http://example.com/test.aspx?a1 http://example.com/test.aspx?b2 http://example.com/test.aspx?c3 http://example.com/test.aspx?a1 http://example.com/test.aspx?b2 http://exampl e.com/test.aspx?a1 http://example.com/test.aspx?a1 1
PowerShell 版本:5.1.17763.592
-InputObject
Specifies the objects to group. Enter a variable that contains the objects, or type a command or expression that gets the objects.
When you use the InputObject parameter to submit a collection of objects to
Group-Object
,Group-Object
receives one object that represents the collection. As a result, it creates a single group with that object as its member.To group the objects in a collection, pipe the objects to
Group-Object
.
因此您需要在作业中使用带有管道的版本:
$summaryUrls = $urls | Group-Object