Kelly Garcia
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 -
$csvb values
Please help!
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
$csvb values
Machine IPaddress
USHUBVCS01 10.0.1.10
USHUBCIS02 10.0.1.22
$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
Please help!
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
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.
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?
ASKER
no there's is duplicates in the imported csv
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
The -or operator isn't used this way. It operates on boolean (true/false) values (expressions)