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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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)
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

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.
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. 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.
aikimarkCommented:
also, are there duplicates in $csvb?
Kelly GarciaSenior Systems AdministratorAuthor Commented:
no there's is duplicates in the imported csv
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.