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!
Kelly GarciaSenior Systems AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
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)
0
aikimarkCommented:
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

0
aikimarkCommented:
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.
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

aikimarkCommented:
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.
0
aikimarkCommented:
also, are there duplicates in $csvb?
0
Kelly GarciaSenior Systems AdministratorAuthor Commented:
no there's is duplicates in the imported csv
0
aikimarkCommented:
I left some comments in there in case I needed to do further testing.
#cd "C:\Users\New User\Downloads"
$csva = import-csv AzureMigrateData.csv   #Q_29113081_
$csvb = import-csv subnets_1.csv          #Q_29113081_

foreach ($b in $csvb) {

	if ($b.Machine -in $csva.machine){

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

		if ($csva[$csva.machine.IndexOf($b.machine)].psobject.properties.value -contains $b.IPaddress){

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

	} 

} #endforeach $csvb

foreach ($a in $csva) {

	if ($a.Machine -notin $csvb.machine){
        $a | export-csv unique.csv -Append -notype
	} 

} #endforeach $csva

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.