Comparing 2 Csv files.


hello experts,

below code , question. 

i comparing 2 csv files , addding flag output csv file value not match , $result.model  and $result.computer contains 2 times repeat data .

#==============================================

$basecsv = import-csv -path "d:\compare files\os1.csv"

$newcsv = import-csv -path "d:\compare files\os2.csv" 

  

$output = @() 

   foreach ($column in $basecsv) {  

 

$result = $newcsv | where-object {$column.computer -eq $_.computer}

 

$oldmodelvalue = $result.model

$modelflagvalue = [string]::compare([string]$column.model, [string]$result.model, $true)

if ($modelflagvalue = '0') {$model = [string]$column.model} else {  $model = $result.model -join "-flag"} 
$result.model

 

write-host $model

$output += new-object psobject -property @{

 

computer = $column.computer

model = $model

 

}

 }

$output | select-object computer,model |export-csv  -path "d:\compare files\change.csv" -notypeinformation -force

 

sample csv file1.

computer,model

abc,sdfhskdfhs

xyz,fdkssdfh

paris,dfdsfkh

 

sample csv file2.

computer,model

abc,sdfhskdfhssad

xyz,fdkssdfhsad

paris,dfdsfkhsad

  

any or pointers pls. 


priyabrata


what wrong code ? output want ?

 $oldcsv = import-csv os1.csv    $newcsv = import-csv os2.csv       $output = @()      foreach ($column in $oldcsv) {            $result = $newcsv | where-object {$column.computer -eq $_.computer}        $model = if ($column.model -ne $result.model) {"found change"}        $output += new-object psobject -property @{        vmname = $column.computer          model = $model        }      }    $output | select-object vmname, model

you can run

compare-object -referenceobject $basecsv -differenceobject $newcsv   -includeequal -property computer,model | { $_.sideindicator -eq '=>' -or $_.sideindicator -eq '==' } | export-csv



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

CRL Revocation always failed

Failed to query the results of bpa xpath

0x300000d errors in Microsoft Remote Desktop client