3

i'm trying to verify if an object exist in a list imported from 2 CSV files I get an error even though the 2 objects are similar

$CSV1 = Import-CSV $path $delimiter $header
$CSV2 = Import-CSV $path2 $delimiter $header


$myList = New-Object -TypeName 'System.Collections.ArrayList';
$myList.add("") | Out-Null
Foreach($value in $CSV1)
{
    $obj = $value.PSObject.Copy()
    $obj.Name = GetNameRef($value.Name)
    $obj.IP = GetIPRef($value.IP)
    if(!($CSV2.contains($obj)))
    {
        $myList.Add($value)|Out-Null
    }
}

$myList | Format-Table * | Out-File ".\Result.txt"

So when i check manually i found a object in CSV2 with the same value for all properties but when i do CSV2.Contains, He didn't return "True"

I know it's like a reference problem but i didn't found any information about that actually or only to compare 1 propertie

The both CSV file has the same template like

First CSV file :

Name,ServerName,IP,Mono 
Luc,PM45,255.245.22.21,MonoY 
Ced,PC78,245.222.1.12,MonoX 

Second CSV file

Name,ServerName,IP,Mono 
John,PM45,255.245.22.20,MonoY 
Dab,PC75,245.222.11.12,MonoX 

In my project for exemple The value Luc and John with IP 255.245.22.21 and 255.245.22.20 refer to the same value, I found reference with GetNameRef() and GetIpRef()

So I modify some value to see if exist in the second CSV file But all value don't have equivalent in second CSVfile and i want found them

Exemple : i have $object1 with value of the first line of CSV file1
i have $object2 with value of the first line of CSV file2
so i have :

$object1 :
Name = Luc,
ServerName = PM45,
IP = 255.245.22.21,
Mono = MonoY

$object2:
Name = John,
ServerName = PM45,
IP = 255.245.22.20,
Mono = MonoY

Now $obj take value of $object1 and i modify Name and IP to obtain this

$object1 :
Name = Luc,
ServerName = PM45,
IP = 255.245.22.21,
Mono = MonoY

$object2:
Name = John,
ServerName = PM45,
IP = 255.245.22.20,
Mono = MonoY

$obj :
Name = John,
ServerName = PM45,
IP = 255.245.22.20,
Mono = MonoY

So $obj and $object2 have same value properties

9
  • pscustomobjects are not comparable. You need to write your own logic for that Commented Dec 28, 2022 at 13:45
  • 1
    Show us a few lines of both CSV files as Formatted text so we know what we're dealing with. Why do you hardcode the name 'John' and the IP '255.255.255.255' in there? Commented Dec 28, 2022 at 13:50
  • Please don't add this information in the comment section, add it to your question instead Commented Dec 28, 2022 at 14:02
  • How sorry , i have edit my post Commented Dec 28, 2022 at 14:09
  • 1
    So, what would you consider equal values? Which fields would you use for that? Obviously not all, because the IP's don't match anywhere.. Commented Dec 28, 2022 at 14:13

1 Answer 1

2

The easiest way you can handle this is probably using ValueTuple or Tuple, both structures are structurally comparable. You can then use a HashSet<T> to filter those objects that appear in CSV1 and CSV2.

Note that this code assumes 2 things:

  1. The objects from both CSVs have the same Property Names (same Column Names).
  2. The objects have 8 properties or less (this is a Tuple and ValueTuple limitation).
$hash = [System.Collections.Generic.HashSet[object]]::new()

foreach($line in Import-Csv path\to\csv1.csv) {
    $null = $hash.Add([ValueTuple]::Create.Invoke($line.PSObject.Properties.Value))
}

Import-Csv path\to\csv2.csv | & {
    process {
        if(-not $hash.Add([ValueTuple]::Create.Invoke($_.PSObject.Properties.Value))) {
            $_
        }
    }
} | Export-Csv path\to\objectsInBothCsvs.csv -NoTypeInformation
Sign up to request clarification or add additional context in comments.

3 Comments

Thank's for giving a way to resolve, i'm gonna try this
That work pretty good thank you so much
@Aomichi im glad it did :) happy to help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.