为什么两个 select 语句(在自定义数组 objects 上)合并为一个结果?

Why are two select statements (on array of custom objects) are combined into one result?

为什么下面的两个 PowerShell“Select Name, Count”语句(来自 objects 的数组)合并为一个结果。我如何修改才能看到两个结果,分组在两个不同的级别。每个 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

--- 更新答案 ---

去掉 Select 并替换为 Format-Table 似乎已经清除了输出。我还摆脱了多余的 Write-Host 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>