Daniel
Daniel

Reputation: 635

Failure to compare values when I am reading from CSV file

My PowerShell script is as follows, I have got all the sites in farm saved as CSV file using Export-Csv command.

$farmList  = Import-Csv "TestFarm.csv"
$farmList1 = Import-Csv "OtherFarm1.csv"
foreach ($site in $farmList)  
{
    Write-Host "db - ", $site
    foreach ($farmsite in $farmList1) 
    {
        if ($site -eq $farmsite) {
            Write-Host "matching site found for ", $farmsite
            break
        }
        Write-Host "farm - ", $farmsite
    }
}   

My Excel files in CSV looks like

Site
/sites/TestSite
/sites/testsite1234
...

The second Excel file in CSV looks like

Site
/sites/TestSite
/sites/testsite1234
...

When I debug the program, I am getting a value of $site and $farmSite as @{Site=/sites/TestSite} , but when I compare the values using -eq, the values do not match.

I have also tried using Compare-Object without success.

Upvotes: 0

Views: 22

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

You need to compare the objects' Site properties instead of the objects themselves.

Change this:

if ($site -eq $farmsite) {

into this:

if ($site.Site -eq $farmsite.Site) {

If the files contain just this one property you could also expand it on import:

$farmList  = Import-Csv "TestFarm.csv" | Select-Object -Expand Site
$farmList1 = Import-Csv "OtherFarm1.csv" | Select-Object -Expand Site

The latter would also allow you to simplify your code by using a -contains check:

foreach ($farmsite in $farmList1) {
    if ($farmList -contains $farmsite) {
        Write-Host "matching site found for $farmsite"
    }
}

Upvotes: 1

Related Questions