question

Ed7 avatar image
0 Votes"
Ed7 asked LimitlessTechnology-2700 edited

compare 2csv whithin if statments

Hello,

I would like to compare 2 csv values (Names and MD5).

The script I do have so far does not what I aim for. I want it to display whether the values match or not and for each action to export as csv file.
Could help me on this please.

Below is my script


$file1 = import-csv "C:\pathtofile.csv"
$file2 = import-csv "C:\Users\users\Desktop\file.csv"

Compare-Object -ReferenceObject $file1 -DifferenceObject $file2 -Property 'Name', 'Hash' -IncludeEqual

first check that properties match (can omit this step if you know for sure they will be)



if(Compare-Object $file1.Hash -eq $file2.Hash)
{
trow "Properties are not the same!" $file1.Hash $file2.Hash
}

pass properties list to Compare-Object

else
{
Compare-Object $file1 $file2 -Property Hash
}

windows-serverwindows-server-powershelloffice-excel-itprooffice-scripts-excel-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

When you post code please use the "Code Sample" editor. The icon is the 5th from the left on the Format Bar and has the graphic "101 010". The normal editor makes a mess of code, as you can see by looking at your post!

In any case, is this more-or-less what you want to do?

 $f1 = [PSCustomObject]@{Hash = "abc"},[PSCustomObject]@{Hash = "def"},[PSCustomObject]@{Hash = "ghi"},[PSCustomObject]@{Hash = "jkl"}
 $f2 = [PSCustomObject]@{Hash = "abc"},[PSCustomObject]@{Hash = "xyz"},[PSCustomObject]@{Hash = "ghi"},[PSCustomObject]@{Hash = "jkl"}
    
 $f1p = $f1.psobject[0].Properties.name | Sort-Object
 $f2p = $f2.psobject[0].Properties.name | Sort-Object
    
 $x = Compare-Object $f1p $f2p
 if ($x.Length -ne 0){
     Throw "Properties are not the same!`r`n$f1p`r`n`r`n$f2p"
 }
    
 Compare-Object $f1 $f2 -IncludeEqual -Property Hash |
     Where-Object {$_.SideIndicator -eq "=="} |                  # just find what's equal
         Select-Object Hash |
             Export-Csv c:\junk\equalvalues.csv -NoTypeInformation
    
 Compare-Object $f1 $f2 -Property Hash |
     Where-Object {$_.SideIndicator -eq "=>"} |
         Select-Object Hash |
             Export-CSV c:\junk\NotInReferenceSet.csv -NoTypeInformation
    
 Compare-Object $f1 $f2 -Property Hash |
     Where-Object {$_.SideIndicator -eq "<="} |
         Select-Object Hash |
             Export-CSV c:\junk\NotInDifferenceSet.csv -NoTypeInformation
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ed7 avatar image
0 Votes"
Ed7 answered RichMatheisen-8856 commented

Hi,

Its nearly there but I need to have the information displayed on the screen and keep the outcomes on a csv file.

The code looks good

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Just add an "Import-CSV" at the end of script for each of the three CSV files that were exported. There's no need to save the results of those Import-CSV's -- just let them be dumped to the console.

0 Votes 0 ·
LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered LimitlessTechnology-2700 edited

Hello @Ed7

I would use the Compare-Object: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/compare-object?view=powershell-7.2&viewFallbackFrom=powershell-6

For example I have this process for disabled users in my AD:

 TEST_AD.csv - this is my csv file export from AD from all my 'active' users. (System of Record).
 TEST_APP.csv - this is my csv file from the application.
 TEST_DISABLED_APP_USERS.csv - this is my csv file that I would upload to the application to disable any users not found in the TEST_AD.csv but are found in the TEST_APP.csv file.
    
 $ADCSV = Import-CSV -Path "C:\temp\TestCSV\TEST_AD.csv"
 $APPCSV = Import-CSV -Path "C:\temp\TestCSV\TEST_APP.csv"
 #for the compare I chose the "Email" header in the CSV file as my 'primary key'.  Something you know will exist to identify items (users) in each file that must exist in both files.
 $compareCSV = Compare-Object $ADCSV $APPCSV -Property Email -IncludeEqual -PassThru
 $output = ForEach ($item in $compareCSV) {
    
     If ($item.SideIndicator -eq "=>") {
         #this item (aka User) exists in the 'difference set' (TEST_APP.csv file) so we'll need to add them to our export CSV.
         [pscustomobject]@{
             #needed to create our output csv file.
             #first column is the export CSV headers, second (after '=') is the values/data.
             #remove any unnecessary headers/values that the upload file doesn't need.
             "Employee ID"     = $item.'Employee ID'
             "First Name" = $item.'First Name'
             "Last Name" = $item.'Last Name'
             "Display Name" = $item.'Display Name'
             "Telephone" = $item.Telephone
             "Email" = $item.Email
             "Street" = $item.Street
             "City" = $item.City
             "State" = $item.State
             "Zip Code" = $item.'Zip Code'
             "Home" = $item.Home
             "Mobile" = $item.Mobile
             "Fax" = $item.Fax
             "Job Title" = $item.'Job Title'
             "Department" = $item.Department
             "Company" = $item.Company
             "Status" = "Disabled" #this is static and should remain. Or set to whatever value you need for our application to disable the user.
         }
     }
 }
 $output | Export-CSV -Path "C:\temp\TestCSV\TEST_DISABLED_APP_USERS.CSV" -NoTypeInformation

Hope this helps with your query,


--If the reply is helpful, please Upvote and Accept as answer--

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.