yep, i dunno if i can make a longer title ... ;)
well, here is what i got :
some lists of objects (lets call them "objA[0..z]") in "listA[1..z]"
some lists of objects (lets call them "objB[0..z]") in "listB[1..z]"
Here is a Dataset (modified from answer below)
$lista1 = @'
[
{
"computerName": "123",
"userName": "Antoine",
"otherData1": "test1.xls",
"otherData2": "yyy"
},
{
"computerName": "123",
"userName": "Antoine",
"otherData1": "test2.xls",
"otherData2": "yyy"
},
{
"computerName": "456",
"userName": "Benoit",
"otherData1": "file1.txt",
"otherData2": "yyy"
}
]
'@
$lista2 = @'
[
{
"computerName": "789",
"userName": "Cyril",
"otherData1": "file1.pps",
"otherData2": "yyy"
},
{
"computerName": "789",
"userName": "Cyril",
"otherData1": "file2.pps",
"otherData2": "yyy"
},
{
"computerName": "321",
"userName": "Damien",
"otherData1": "doc.docx",
"otherData2": "yyy"
}
]
'@
$listb1 = @'
[
{
"computerName": "789",
"userName": "Cyril",
"otherData1": "file2.pps",
"otherData2": "yyy"
},
{
"computerName": "321",
"userName": "Damien",
"otherData1": "doc.docx",
"otherData2": "bbb"
}
]
'@
$listb2 = @'
[
{
"computerName": "123",
"userName": "Antoine",
"otherData1": "test1.xls",
"otherData2": "yyy"
},
{
"computerName": "123",
"userName": "Antoine",
"otherData1": "test2.xls",
"otherData2": "yyy"
},
]
'@
# expected output with this dataset :
# 1st and 2nd are not in any listb and 3rd.otherData2 do not match with each other
[
{
"computerName": "456",
"userName": "Benoit",
"otherData1": "file1.txt",
"otherData2": "yyy"
},
{
"computerName": "789",
"userName": "Cyril",
"otherData1": "file1.pps",
"otherData2": "yyy"
},
{
"computerName": "321",
"userName": "Damien",
"otherData1": "doc.docx",
"otherData2": "yyy"
}
]
what i'm trying to do :
If (obj'w' in lista'x') match computerName and userName and otherData1 in (obj'y' in listb'z')
AND IF same object NOT match otherData2 -> return said object
ELSE IF (obj'w' in lista'x') NOT match any object in every listb[0..z] -> return said object
here is what i did so far and where i'm stuck.
I more or less managed to make it work with "single file" on both listA & listB
Problem1 is i'm clearly drowing in foreach loop and i fail to find a "clean way".
Problem2 is i fail to properly check if objA is present in listB resulting in not being able to return it. (or returning all objects)
$listAPath = 'D:\A'
$listBPath = 'D:\B'
# multiples files in each directory
$listAFiles = Get-ChildItem $listAPath
$listBFiles = Get-ChildItem $listBPath
$failList = foreach ($listAFile in $listAFiles) {
$listObjA = Get-Content $listAFile.fullname -raw | ConvertFrom-Json
foreach ($objA in $listObjA) {
foreach ($listBFile in $listBFiles) {
$listObjB = Get-Content $listBFile.fullname -raw | ConvertFrom-Json
foreach ($objB in $listObjB) {
if ($objA.computerName -eq $objB.computerName -and $objA.userName -eq $objB.userName -and $objA.otherData1 -eq $objB.otherData1) {
if (<# do something with OtherData2 #>) {
$ObjA
}
} <# else {return $objA} #>
}
}
}
}
$failList
Reworked this script while typing it, hope i did not forget anything while doing it.
I'd be glad if i could get help here
If you need some more information, do not hesitate to ask.
Ty in advance.
EDITED : trying to apply what was asked in comment / answer, did not modify first "version" of script provided even though it might no match anymore with given dataset and reworked informations... will edit if asked.
For thoose who are curious, otherData2 is a datetime data with the "real" script.