首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么两个select语句(关于自定义对象的数组)被组合成一个结果?

为什么两个select语句(关于自定义对象的数组)被组合成一个结果?
EN

Stack Overflow用户
提问于 2022-01-04 21:28:18
回答 1查看 45关注 0票数 0

为什么下面的两个PowerShell "Select,Count“语句(来自一个对象数组)组合成一个结果。如何修改,以看到两个结果,分组在两个不同的水平。每个select都返回正确的数据,但是它们被合并成一组常见的标题。

代码语言:javascript
复制
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 

实际输出:

代码语言:javascript
复制
============================

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

期望输出:

代码语言:javascript
复制
============================

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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-05 01:07:10

--最新答案

摆脱选择和替换格式-表似乎清除了输出。我也摆脱了多余的写主机cmds。

我清理了您的示例,使每个对象都具有相同的属性。然后我用一个中间变量分解了管道,它似乎工作得很好。

代码语言:javascript
复制
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 

样本输出:

代码语言:javascript
复制
============================

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> 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70585291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档