为什么下面的两个PowerShell "Select,Count“语句(来自一个对象数组)组合成一个结果。如何修改,以看到两个结果,分组在两个不同的水平。每个select都返回正确的数据,但是它们被合并成一组常见的标题。
cls
$customObjectsArray = @()
#### build a few objects for to reproduce my issue ####
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo1.odx"
Type = "Orchestration"
Method = "GetText"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo1.odx"
Type ="Orchestration"
Method = "GetUser"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo2.btm"
Type = "Map"
Method = "GetText"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileType = "Demo2.btm"
Type = "Map"
Method = "GetConnString"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo4.odx"
Type = "Orchestration"
Method = "GetText"
}
$customObjectsArray += $customObject
Write-Host ""
Write-Host "============================"
Write-Host ""
Write-Host "Grouped By MethodName, FileType... Count of Items = $($customObjectsArray.Count)"
$customObjectsArray | Group Method, Type | Sort Count -Descending | Select Name, Count
# I tried this way as well, same result.
#$groupdArray2 = $customObjectsArray | Group Method, Type | Sort Count -Descending
#$result1 = $groupedArray2 | Select Name, Count
#$result1
Write-Host ""
Write-Host "============================"
Write-Host ""
Write-Host "Grouped By MethodName Only ... Count of Items = $($customObjectsArray.Count)"
$customObjectsArray | Group Method | Sort Count -Descending | Select Name, Count
# I tried this way as well, same result.
#$groupedArray1 = $customObjectsArray | Group Method | Sort Count -Descending
#$result2 = $groupedArray1 | Select Name, Count
#$result2 实际输出:
============================
Grouped By MethodName, FileType... Count of Items = 5
============================
Grouped By MethodName Only ... Count of Items = 5
Name Count
---- -----
GetText, Orchestration 2
GetUser, Orchestration 1
GetText, Map 1
GetConnString, Map 1
GetText 3
GetUser 1
GetConnString 1期望输出:
============================
Grouped By MethodName, FileType... Count of Items = 5
Name Count
---- -----
GetText, Orchestration 2
GetUser, Orchestration 1
GetText, Map 1
GetConnString, Map 1
============================
Grouped By MethodName Only ... Count of Items = 5
Name Count
---- -----
GetText 3
GetUser 1
GetConnString 1发布于 2022-01-05 01:07:10
--最新答案
摆脱选择和替换格式-表似乎清除了输出。我也摆脱了多余的写主机cmds。
我清理了您的示例,使每个对象都具有相同的属性。然后我用一个中间变量分解了管道,它似乎工作得很好。
cls
$customObjectsArray = @()
#### build a few objects for to reproduce my issue ####
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo1.odx"
Type = "Orchestration"
Method = "GetText"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo1.odx"
Type = "Orchestration"
Method = "GetUser"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo9.btm"
Type = "Map"
Method = "GetText"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo2.btm"
Type = "Map"
Method = "GetConnString"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo4.odx"
Type = "Orchestration"
Method = "GetText"
}
$customObjectsArray += $customObject
"`n`n============================`n"
'Grouped By MethodName... Count of Items = $($customObjectsArray.Count)'
$Tally = $customObjectsArray | Group Method
$Tally | Sort Count -Descending | FT Name, Count
"`n`n============================`n"
'Grouped By MethodName, FileType... Count of Items = $($customObjectsArray.Count)'
$Tally2 = $customObjectsArray | Group Method, Type
$Tally2 | Sort Count -Descending | Ft Name, Count 样本输出:
============================
Grouped By MethodName... Count of Items = $($customObjectsArray.Count)
Name Count
---- -----
GetText 3
GetUser 1
GetConnString 1
============================
Grouped By MethodName, FileType... Count of Items = $($customObjectsArray.Count)
Name Count
---- -----
GetText, Orchestration 2
GetUser, Orchestration 1
GetText, Map 1
GetConnString, Map 1
PS> https://stackoverflow.com/questions/70585291
复制相似问题