I am looking for a way to filter nested objects with PowerShell. Let's say I have a list that contains UPN addresses.
$UPN = "user1@xyz.com", "user5@xyz.com"
In the below object converted to JSON, I want to get only those objects where "Attr1" within whole object contains any address from the above list.
[
{
"Id": "1",
"Name": "XYZ",
"Attr1": [
{
"Id": "1",
"UPN": "user1@xyz.com"
},
{
"Id": "2",
"UPN": "user2@xyz.com"
},
{
"Id": "3",
"UPN": "user3@xyz.com"
}
],
"Attr2": [
{
"Id": "1",
[...]
}
],
"Attr3": [
{
"Id": "1",
[...]
},
{
"Id": "2",
[...]
}
]
},
{
"Id": "2",
"Name": "XYZ",
"Attr1": [
{
"Id": "1",
"UPN": "user2@xyz.com"
}
],
"Attr2": [
],
"Attr3": [
{
"Id": "1",
[...]
}
]
}
]
How to filter such an object? The standard method to not work for me (I am not getting the desired result, losing data):
$CollectionOfObjects | Where-Object {$_.Attr1.UPN -in $UPN}
How to handle this?
Regards!