比较对象查找匹配项并从第一个对象中删除找到的内容
Compare-Object Find Matches and Remove Found from First Object
我想知道是否有更简单的方法来完成此操作。我有两个 (JSON) 对象,它们的属性是 IP 列表(属性是单个 IP)。我正在比较两个对象属性以查找任何匹配项,并希望从第一个对象 ($JSONConverted
) 中删除所有匹配项。我相信我可以使用删除功能(我还没有开始工作)。我真的很想知道是否有更简单的方法来完成这个。
$JSONConverted = Get-Content -Raw -Path Output.json | ConvertFrom-Json
$FWJSONConverted = Get-Content -Raw -Path FWOutput.json | ConvertFrom-Json
$MatchingIPs = Compare-Object -IncludeEqual -ExcludeDifferent -ReferenceObject $FWJSONConverted.data.value -DifferenceObject $JSONConverted.data.value
$ListOfMatchingIPs = $MatchingIPs.InputObject
$JSONConverted.data.value | ForEach-Object {
foreach ($IP in $ListOfMatchingIPs) {
if ($_ -eq $IP) {
$JSONConverted.remove.($_)
}
}
}
这是 $JSONConverted
数据的示例:
{
"number_of_elements": 1134,
"timeout_type": "LAST",
"name": "IP List",
"data": [
{
"last_seen": 1486571563476,
"source": "WORD: WORDS",
"value": "10.10.10.10",
"first_seen": 1486397213696
},
{
"last_seen": 1486736205285,
"source": "WORD: WORDS",
"value": "10.17.24.22",
"first_seen": 1486397813280
},
{
"last_seen": 1486637743793,
"source": "WORD: WORDS",
"value": "10.11.10.10",
"first_seen": 1486398713056
}
],
"creation_time": 1486394698941,
"time_to_live": "1 years 0 mons 3 days 0 hours 0 mins 0.00 secs",
"element_type":"IP"
}
像这样的东西就足够了(假设你想从 data
数组中删除整个子对象):
$JSONConverted.data = $JSONConverted.data | Where-Object {
@($FWJSONConverted.data.value) -notcontains $_.value
}
我想知道是否有更简单的方法来完成此操作。我有两个 (JSON) 对象,它们的属性是 IP 列表(属性是单个 IP)。我正在比较两个对象属性以查找任何匹配项,并希望从第一个对象 ($JSONConverted
) 中删除所有匹配项。我相信我可以使用删除功能(我还没有开始工作)。我真的很想知道是否有更简单的方法来完成这个。
$JSONConverted = Get-Content -Raw -Path Output.json | ConvertFrom-Json
$FWJSONConverted = Get-Content -Raw -Path FWOutput.json | ConvertFrom-Json
$MatchingIPs = Compare-Object -IncludeEqual -ExcludeDifferent -ReferenceObject $FWJSONConverted.data.value -DifferenceObject $JSONConverted.data.value
$ListOfMatchingIPs = $MatchingIPs.InputObject
$JSONConverted.data.value | ForEach-Object {
foreach ($IP in $ListOfMatchingIPs) {
if ($_ -eq $IP) {
$JSONConverted.remove.($_)
}
}
}
这是 $JSONConverted
数据的示例:
{
"number_of_elements": 1134,
"timeout_type": "LAST",
"name": "IP List",
"data": [
{
"last_seen": 1486571563476,
"source": "WORD: WORDS",
"value": "10.10.10.10",
"first_seen": 1486397213696
},
{
"last_seen": 1486736205285,
"source": "WORD: WORDS",
"value": "10.17.24.22",
"first_seen": 1486397813280
},
{
"last_seen": 1486637743793,
"source": "WORD: WORDS",
"value": "10.11.10.10",
"first_seen": 1486398713056
}
],
"creation_time": 1486394698941,
"time_to_live": "1 years 0 mons 3 days 0 hours 0 mins 0.00 secs",
"element_type":"IP"
}
像这样的东西就足够了(假设你想从 data
数组中删除整个子对象):
$JSONConverted.data = $JSONConverted.data | Where-Object {
@($FWJSONConverted.data.value) -notcontains $_.value
}