Home > Software engineering >  Look if a object exist in a list powershell
Look if a object exist in a list powershell

Time:01-04

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

CodePudding user response:

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
  • Related