lundi 3 octobre 2016

Variables seem the same but don't match PowerShell

I have been breaking my head over this.

I am trying to compare 2 values in variables to see if they're the same, in the powershell output I can see that some combinations should be true!

First, without the making of $vergelijking1 and $vergelijking2 it showed as if $nummersPOs[$counter] and $object.'col1' were the same but the if statement was never true.

The only thing I could think of as to why it would fail is that 1 of the variables comes from an array. When I changed both types to String I could indeed see that there was some hidden text but I don't understand why my if statment is never true now. It writes "test2" but should write "inside the loop". I am new to PowerShell but would really like to make this work.

[System.Collections.ArrayList]$data=Import-Csv "C:\Users\UserName\Documents\test.csv"
[System.Collections.ArrayList]$NummersPOs = Import-Csv "C:\Users\UserName\Documents\test.csv" | select "col1" -Unique
$counter = 0

foreach($object in $NummersPOs){
        $newCSV=New-Object System.Collections.ArrayList
        foreach ($object in $data){
            if ($object."col2"){
            $index = $newCSV.add($object)
            [string]$vergelijking1 = $NummersPOs[$counter]
            #$vergelijking1 = $vergelijking1 -replace '\D+(\d+)','$1'
            $vergelijking1
            [string]$vergelijking2 = $object.'col1'
            $vergelijking2
                if($vergelijking1 -contains $vergelijking2){
                    Write-Host "inside the loop"
                } else {
                    write-host "test2"
                }
            }

        }
$counter++
}
$newCSV | Export-Csv "C:\Users\UserName\Documents\test2.csv"

Sample output: @{col1=632424} 632424 test2 @{col1=632424} 632446 test2

As you can see, the first one should have been true already. -contain or -like both give false btw.

Aucun commentaire:

Enregistrer un commentaire