Return 使用 PowerShell 由属性指定的唯一对象数组
Return an array of unique objects which are specified by properties using PowerShell
我有一个从 JSON 转换而来的对象,其中包含有关 'Trusted clients' 的信息。有两个属性,其中一个标识客户端的唯一性:Name
AND Thumbprint
,所以如果有元素,要么包含相同的Name
或 Thumbprint
,它们必须被省略。
另外,我想指出输出数组应该包含额外的 属性 - Id
可以用 - New-Guid
生成。此处讨论了该特殊情况:.
{
"TrustedClients": [
{
"Name": "Client1",
"Thumbprint": "5ed7eb688e404bd787585637975ddb01",
"CallbackThumbprint": "b7f610106fa24afe9460ab8e4f2db1fc"
},
{
"Name": "Client2",
"Thumbprint": "5ed7eb688e404bd787585637975ddb01",
"CallbackThumbprint": "b7f610106fa24afe9460ab8e4f2db1fc"
},
{
"Name": "Client3",
"Thumbprint": "1700a8497495d6053be04b690b98479fd62e6cc9",
"CallbackThumbprint": "b7f610106fa24afe9460ab8e4f2db1fc"
}
]
}
是否有有效的方法来获取由 Name
AND Thumbprint
属性指定的唯一对象数组?所以在那种情况下,输出数组应该只包含一个名称为 Client3
的对象,因为有两个元素具有相同的 Thumbprint
值:
{
"TrustedClients": [
{
"Id": "{(New-Guid).ToString()}"
"Name": "Client3",
"Thumbprint": "1700a8497495d6053be04b690b98479fd62e6cc9",
"CallbackThumbprint": "b7f610106fa24afe9460ab8e4f2db1fc"
}
]
}
一开始我用 grouping 进行了调整,但它与基本思想冲突 - Name
AND [的唯一性] Thumbprint
。很明显 分组 元素 Name
AND Thumbprint
可能 return 具有不同 Name
值,但相同 Thumbprint
反之亦然。
我的看法是 Group-Object
2 次,在 Name
和 Thumbprint
上,以获得那些重复的对象,一旦我们有了这些信息,我们可以通过过滤 object[]
.
来跳过它们
我在这种情况下使用 -NoElement
以提高效率。
因为我完全是 类 的菜鸟,所以我决定制作一个用于练习目的:)
class UniqueJSON {
[object[]]$Items
UniqueJSON ([pscustomobject[]]$In) {
$this.Items = $In
$this.SetGuid()
}
[void]SetGuid () {
$this.Items.Where({ -not $_.ID }).ForEach({
$_.PSObject.Properties.Add([psnoteproperty]::new(
'ID', [guid]::NewGuid().Guid
)
)
})
}
[object[]]GetUnique() {
$thumbNotShow = $this.Items | Group-Object Thumbprint -NoElement |
Where-Object Count -GT 1
$nameNotShow = $this.Items | Group-Object Name -NoElement |
Where-Object Count -GT 1
return $this.Items.Where({
$_.Name -notin $nameNotShow.Name -and
$_.Thumbprint -notin $thumbNotShow.Name
})
}
}
# Here you can import your JSON
$json = ..... | ConvertFrom-Json
# Now store the TrustedClients property in a variable
$trusted = $json.TrustedClients
# Create a new instance of the class
$instance = [UniqueJSON]$trusted
# Get the unique results
$instance.GetUnique()
$instance.GetUnique()
returns:
Name Thumbprint CallbackThumbprint ID
---- ---------- ------------------ --
Client3 1700a8497495d6053be04b690b98479fd62e6cc9 b7f610106fa24afe9460ab8e4f2db1fc 85af5623-365c-4f8a-9863-be38860406da
我有一个从 JSON 转换而来的对象,其中包含有关 'Trusted clients' 的信息。有两个属性,其中一个标识客户端的唯一性:Name
AND Thumbprint
,所以如果有元素,要么包含相同的Name
或 Thumbprint
,它们必须被省略。
另外,我想指出输出数组应该包含额外的 属性 - Id
可以用 - New-Guid
生成。此处讨论了该特殊情况:
{
"TrustedClients": [
{
"Name": "Client1",
"Thumbprint": "5ed7eb688e404bd787585637975ddb01",
"CallbackThumbprint": "b7f610106fa24afe9460ab8e4f2db1fc"
},
{
"Name": "Client2",
"Thumbprint": "5ed7eb688e404bd787585637975ddb01",
"CallbackThumbprint": "b7f610106fa24afe9460ab8e4f2db1fc"
},
{
"Name": "Client3",
"Thumbprint": "1700a8497495d6053be04b690b98479fd62e6cc9",
"CallbackThumbprint": "b7f610106fa24afe9460ab8e4f2db1fc"
}
]
}
是否有有效的方法来获取由 Name
AND Thumbprint
属性指定的唯一对象数组?所以在那种情况下,输出数组应该只包含一个名称为 Client3
的对象,因为有两个元素具有相同的 Thumbprint
值:
{
"TrustedClients": [
{
"Id": "{(New-Guid).ToString()}"
"Name": "Client3",
"Thumbprint": "1700a8497495d6053be04b690b98479fd62e6cc9",
"CallbackThumbprint": "b7f610106fa24afe9460ab8e4f2db1fc"
}
]
}
一开始我用 grouping 进行了调整,但它与基本思想冲突 - Name
AND [的唯一性] Thumbprint
。很明显 分组 元素 Name
AND Thumbprint
可能 return 具有不同 Name
值,但相同 Thumbprint
反之亦然。
我的看法是 Group-Object
2 次,在 Name
和 Thumbprint
上,以获得那些重复的对象,一旦我们有了这些信息,我们可以通过过滤 object[]
.
我在这种情况下使用 -NoElement
以提高效率。
因为我完全是 类 的菜鸟,所以我决定制作一个用于练习目的:)
class UniqueJSON {
[object[]]$Items
UniqueJSON ([pscustomobject[]]$In) {
$this.Items = $In
$this.SetGuid()
}
[void]SetGuid () {
$this.Items.Where({ -not $_.ID }).ForEach({
$_.PSObject.Properties.Add([psnoteproperty]::new(
'ID', [guid]::NewGuid().Guid
)
)
})
}
[object[]]GetUnique() {
$thumbNotShow = $this.Items | Group-Object Thumbprint -NoElement |
Where-Object Count -GT 1
$nameNotShow = $this.Items | Group-Object Name -NoElement |
Where-Object Count -GT 1
return $this.Items.Where({
$_.Name -notin $nameNotShow.Name -and
$_.Thumbprint -notin $thumbNotShow.Name
})
}
}
# Here you can import your JSON
$json = ..... | ConvertFrom-Json
# Now store the TrustedClients property in a variable
$trusted = $json.TrustedClients
# Create a new instance of the class
$instance = [UniqueJSON]$trusted
# Get the unique results
$instance.GetUnique()
$instance.GetUnique()
returns:
Name Thumbprint CallbackThumbprint ID
---- ---------- ------------------ --
Client3 1700a8497495d6053be04b690b98479fd62e6cc9 b7f610106fa24afe9460ab8e4f2db1fc 85af5623-365c-4f8a-9863-be38860406da