My objective is to compare two CSV files, and I want to pinpoint the "result" of a mismatch in the specific columns in both CSV and having a hard time.
I have a Master CSV, and all columns for each value should match up on the Site. Also, if the entire record is missing in Master.csv and is in the Site.CSV, I want the result.
I tried "compare-object," and it did not give me what I wanted, so I went with the below.
#$CSV1 = Import-Csv C:\temp\Master.csv
$csv1 = ConvertFrom-Csv @'
site, name, description, detection
New York, Test1, Testing Something, ""
New York, Test2, Doing Something, ""
New York, Test3, "" , TRUE
New York, Test4, "" , ""
New York, Test6, bla bla , FALSE
'@
#$CSV2 = Import-Csv C:\temp\Site.csv
$CSV2 = ConvertFrom-Csv @'
site, name, description, detection
London, Test1, Testing Something, ""
London, Test5, "", ""
London, Test3, "", ""
London, Test2, Doing Someth, TRUE
'@
$Count = $CSV2.Count
$Results = For ($i = 0; $i -lt $Count; $i++) {
If ($CSV2[$i].name -ne $CSV1[$i].name) {
$Match = "Missing Name: '$($CSV2[$i].name)' on '$($CSV1[$i].site)'"
} elseif ($CSV2[$i].name -eq $CSV1[$i].name -and $CSV2[$i].description -ne $CSV1[$i].description -and $CSV2[$i].detection -eq $CSV1[$i].detection) {
$Match = "Missing description: '$($CSV2[$i].description)' on '$($CSV1[$i].site)'"
} elseif ($CSV2[$i].name -eq $CSV1[$i].name) {
$Match = "Match on $($CSV2[$i].site)"
}
Else {
$Match = "Not Match on $($CSV2[$i].site)"
}
[PSCustomObject]@{
site = $CSV2[$i].site
name = $CSV2[$i].name
description = $CSV2[$i].description
detection = $CSV2[$i].detection
Results = $Match
}
}
$Results
I want the results below; when running all code above, for the code above I get the same results in multiple rows
site : London
name : Test1
description : Testing Something
detection :
Results : Both Match London and New York
site : London
name : Test2
description : Doing Someth
detection : true
Results : Description missmatch 'Doing Something', detection missmatch
site : London
name : Test3
description :
detection :
Results : Missing detection 'True'
site : New York
name : Test5
description :
detection :
Results : Record 'Test5' Missing
site : London
name : Test6
description : bla bla
detection : false
Results : Record 'Test6' Missing