Link to home
Start Free TrialLog in
Avatar of Kelly Garcia
Kelly GarciaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Powershell CSV - check duplicates

Hi,

I have this powershell script to check for duplicates. The problem is when it outputs the non duplicates it does this 3 times.

$csva.length is 2
$csvb.length is 4

$csva values -

Machine	IPaddress1	IPaddress2	IPaddress3
USHUBVCS01	10.0.1.10	10.0.1.11	10.0.1.12
USHUBCIS02	10.0.1.21	10.0.1.22	10.0.1.24

Open in new window



$csvb values

Machine	IPaddress
USHUBVCS01	10.0.1.10
USHUBCIS02	10.0.1.22

Open in new window



$csva=import-csv AzureMigrate_1.csv
$csvb=import-csv subnets_1.csv


foreach ($a in $csva) {

$dataa = $a

#echo $dataa

foreach ($b in $csvb) {

if ($dataa.machine -eq $b.Machine){

    write-host "Duplicate Found $($dataa.machine) checking IPs...." -ForegroundColor Green

    if ($dataa.IPaddress1 -or $dataa.IPaddress2 -or $dataa.IPaddress3 -or $dataa.IPaddress4 -or $dataa.IPaddress5 -or $dataa.IPaddress6 -or $dataa.IPaddress7 -or $dataa.IPaddress8 -eq $b.IPaddress){

        write-host "Duplicate IP Found $($b.ipaddress) " -ForegroundColor Green
        $b | Export-Csv duplicates.csv -Append
    }

} Else { $dataa | export-csv unique.csv -Append }

} #endforeach
} #endforeach

Open in new window


Please help!
Avatar of aikimark
aikimark
Flag of United States of America image

Here's an easier-to-read version of the code.
$csva=import-csv AzureMigrate_1.csv
$csvb=import-csv subnets_1.csv

foreach ($a in $csva) {

	$dataa = $a

	#echo $dataa

	foreach ($b in $csvb) {

		if ($dataa.machine -eq $b.Machine){

			write-host "Duplicate Found $($dataa.machine) checking IPs...." -ForegroundColor Green

			if ($dataa.IPaddress1 -or $dataa.IPaddress2 -or $dataa.IPaddress3 -or $dataa.IPaddress4 -or $dataa.IPaddress5 -or $dataa.IPaddress6 -or $dataa.IPaddress7 -or $dataa.IPaddress8 -eq $b.IPaddress){

				write-host "Duplicate IP Found $($b.ipaddress) " -ForegroundColor Green
				$b | Export-Csv duplicates.csv -Append
			}	

		} 
		Else { $dataa | export-csv unique.csv -Append }

	} #endforeach
} #endforeach

Open in new window

The -or operator isn't used this way.  It operates on boolean (true/false) values (expressions)
The first workable solution might look like this:
@($dataa.IPaddress1, $dataa.IPaddress2, $dataa.IPaddress3, 
  $dataa.IPaddress4, $dataa.IPaddress5, $dataa.IPaddress6, 
  $dataa.IPaddress7, $dataa.IPaddress8) -contains $b.IPaddress

Open in new window

I'm not pleased with the elegance and simplicity of that solution.  I think there'll be something really nice after some thought and play with the code.
Are there any duplicates in the $csva values?

If your lists are large, it would behoove you to populate a hash table variable and do your look-ups that way.
also, are there duplicates in $csvb?
Avatar of Kelly Garcia

ASKER

no there's is duplicates in the imported csv
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial