Powershell 分组在不同版本中的行为不同
Powershell Grouping acting differently in different versions
问题:我该如何写才能在 v4 和 v5 中给出相同的结果?
我正在尝试按 SiteCode
对以下数据集进行分组。
我有一个数据集如下[哈希数组]:
假设 AppointmentId
总是唯一的
$groupedDataset = @{}
$dataset = @(
@{
Program = "x"
AppointmentId = "1234567891"
AdminDate = "x"
CountryName = "x"
SiteCode = "x1111"
DateRequested = "x"
SubjectID = "x"
AccountID = "x"
},
@{
Program = "x"
AppointmentId = "1234567892"
AdminDate = "x"
CountryName = "x"
SiteCode = "x1112"
DateRequested = "x"
SubjectID = "x"
AccountID = "x"
},
@{
Program = "x"
AppointmentId = "1234567893"
AdminDate = "x"
CountryName = "x"
SiteCode = "x1113"
DateRequested = "x"
SubjectID = "x"
AccountID = "x"
},
@{
Program = "x"
AppointmentId = "1234567894"
AdminDate = "x"
CountryName = "x"
SiteCode = "x1111"
DateRequested = "x"
SubjectID = "x"
AccountID = "x"
}
)
当我运行下面的代码在PS Version: 5.1
$dataset |
ForEach-Object { [PSCustomObject]$_ } |
Group-Object -Property SiteCode |
ForEach-Object {
$groupedDataset[$_.Name] = $_.Group
}
它returns我要求的结果:
Name Value
---- -----
x1113 {@{SiteCode=x1113; Program=x; Appointment...
x1111 {@{SiteCode=x1111; Program=x; Appointment...
x1112 {@{SiteCode=x1112; Program=x; Appointment...
当我 运行 PS Version: 4.0
中的 精确 代码时,它 returns 以下内容:
Name Value
---- -----
{System.Collections.Hashtable, System.Collect...
在 PowerShell v5.0 中进行了改进,例如。 PSCustomObject 转换,在您的脚本中
ForEach-Object { [PSCustomObject]$_ }
尝试用老派的方法来做,不要自己构建哈希表,而是使用 groupby 返回的哈希表。
$groupedDataset = $dataset |
ForEach-Object {
[PSCustomObject]@{
Program = $_.Program
AppointmentId = $_.AppointmentId
AdminDate = $_.AdminDate
CountryName = $_.CountryName
SiteCode = $_.SiteCode
DateRequested = $_.DateRequested
SubjectID = $_.SubjectID
AccountID = $_.AccountID
}
} |
Group-Object -Property SiteCode -AsHashTable
这导致
$groupedDataset | out-host
Count Name Group
----- ----- ------
2 x1111 {@{Program=x; AppointmentId=1234567891; AdminDate=x; CountryName=x; SiteCode=x1111; DateRequested=x; SubjectID=x; AccountID=x}, @{Program=x; AppointmentId=1234567894; AdminDate=x; CountryN...
1 x1112 {@{Program=x; AppointmentId=1234567892; AdminDate=x; CountryName=x; SiteCode=x1112; DateRequested=x; SubjectID=x; AccountID=x}}
1 x1113 {@{Program=x; AppointmentId=1234567893; AdminDate=x; CountryName=x; SiteCode=x1113; DateRequested=x; SubjectID=x; AccountID=x}
问题:我该如何写才能在 v4 和 v5 中给出相同的结果?
我正在尝试按 SiteCode
对以下数据集进行分组。
我有一个数据集如下[哈希数组]:
假设 AppointmentId
总是唯一的
$groupedDataset = @{}
$dataset = @(
@{
Program = "x"
AppointmentId = "1234567891"
AdminDate = "x"
CountryName = "x"
SiteCode = "x1111"
DateRequested = "x"
SubjectID = "x"
AccountID = "x"
},
@{
Program = "x"
AppointmentId = "1234567892"
AdminDate = "x"
CountryName = "x"
SiteCode = "x1112"
DateRequested = "x"
SubjectID = "x"
AccountID = "x"
},
@{
Program = "x"
AppointmentId = "1234567893"
AdminDate = "x"
CountryName = "x"
SiteCode = "x1113"
DateRequested = "x"
SubjectID = "x"
AccountID = "x"
},
@{
Program = "x"
AppointmentId = "1234567894"
AdminDate = "x"
CountryName = "x"
SiteCode = "x1111"
DateRequested = "x"
SubjectID = "x"
AccountID = "x"
}
)
当我运行下面的代码在PS Version: 5.1
$dataset |
ForEach-Object { [PSCustomObject]$_ } |
Group-Object -Property SiteCode |
ForEach-Object {
$groupedDataset[$_.Name] = $_.Group
}
它returns我要求的结果:
Name Value
---- -----
x1113 {@{SiteCode=x1113; Program=x; Appointment...
x1111 {@{SiteCode=x1111; Program=x; Appointment...
x1112 {@{SiteCode=x1112; Program=x; Appointment...
当我 运行
PS Version: 4.0
中的 精确 代码时,它 returns 以下内容:
Name Value
---- -----
{System.Collections.Hashtable, System.Collect...
在 PowerShell v5.0 中进行了改进,例如。 PSCustomObject 转换,在您的脚本中
ForEach-Object { [PSCustomObject]$_ }
尝试用老派的方法来做,不要自己构建哈希表,而是使用 groupby 返回的哈希表。
$groupedDataset = $dataset |
ForEach-Object {
[PSCustomObject]@{
Program = $_.Program
AppointmentId = $_.AppointmentId
AdminDate = $_.AdminDate
CountryName = $_.CountryName
SiteCode = $_.SiteCode
DateRequested = $_.DateRequested
SubjectID = $_.SubjectID
AccountID = $_.AccountID
}
} |
Group-Object -Property SiteCode -AsHashTable
这导致
$groupedDataset | out-host
Count Name Group
----- ----- ------
2 x1111 {@{Program=x; AppointmentId=1234567891; AdminDate=x; CountryName=x; SiteCode=x1111; DateRequested=x; SubjectID=x; AccountID=x}, @{Program=x; AppointmentId=1234567894; AdminDate=x; CountryN...
1 x1112 {@{Program=x; AppointmentId=1234567892; AdminDate=x; CountryName=x; SiteCode=x1112; DateRequested=x; SubjectID=x; AccountID=x}}
1 x1113 {@{Program=x; AppointmentId=1234567893; AdminDate=x; CountryName=x; SiteCode=x1113; DateRequested=x; SubjectID=x; AccountID=x}