0

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

Hank
  • 19
  • 6
  • 4
    You forgot to specify your problem and/or ask a question. Showing results and letting people interpret them is not something you should do. – Destroy666 Mar 25 '23 at 21:18
  • 3
    Why tag all PowerShell versions you could find? What version(s) are you actually using? And is your issue actually specific to one of them? – Olaf Mar 25 '23 at 23:40
  • You want a "Left Outer Join" so when the object on the right side of the join and not left you still get results. There is no join in PS. I usually create a loop that take takes items missing items on the right side of join and add to results. – jdweng Mar 26 '23 at 08:48
  • Appearently you want to do a full join on the name and get an overview what is different. Using this [`Join-Object script`](https://www.powershellgallery.com/packages/Join)/[`Join-Object Module`](https://www.powershellgallery.com/packages/JoinModule) (see also: [In Powershell, what's the best way to join two tables into one?](https://stackoverflow.com/a/45483110/1701026)): `Import-Csv .\Master.Csv |FullJoin (Import-Csv .\Site.Csv) -on Name -Name Master,Site`. Based on the result, you might than build your own result message. – iRon Mar 26 '23 at 09:34

0 Answers0