0

So I read stackoverflow a lot and it always helps me with my understanding, at the moment, I'm a bit stumped on the matching any of multiple variables and as such, thought I'd ask the exports.

A bit of background on my task.

I am a systems admin for an AD forest with multiple child domains (+4), we are going through a process to consolidate these domains into one - however, unfortunately, as each was managed independently we have comes across the age old 'duplicate SAM' issue (and will for sure come across dupe UPNs too once we start digging).

As such - I am trying to build a script to find these duplicates so I can address them with the teams who support the users in these domains.

I've got a fair way through this, I've exported a list of the SAMs for one migrating domain, and I can parse this into PS and have it look up each SAM against all of my domains and either give me a variable with the username OR $null.

So, I now just want to understand what the 'IF' command is for matching more than one variable and showing which ones match.

So, for example, from these variables...

$one="MSmith01"
$oneNT="DOMAIN1\MSmith01"
$two="MSmith01"
$twoNT="DOMAIN2\MSmith01"
$three=$null
$threeNT=$null
$four=$null
$fourNT=$null
$five="MSmith01"
$fiveNT="DOMAIN5\MSmith01"
$six=$null
$sixNT=$null

How do I write the IF command to show me if more than one variable matches??

If I write this:

if ($one -match $two -match $three -match $four -match $five -match $six) {write-host "Duplicate Found - $oneNT, $twoNT, $threeNT, $fourNT, $fiveNT, $sixNT." -foregroundcolor "yellow"}

It will show me :

Duplicate Found - DOMAIN1\MSmith01, DOMAIN2\MSmith01, , , DOMAIN5\MSmith01,.

But, if the variables are:

$one=$null
$oneNT=$null
$two="MSmith01"
$twoNT="DOMAIN2\MSmith01"
$three="MSmith01"
$threeNT="DOMAIN3\MSmith01"
$four=$null
$fourNT=$null
$five=$null
$fiveNT=$null
$six=$null
$sixNT=$null

Then the result seems to ignore the fact that DOMAIN 2 and DOMAIN3 have matching SAMs and it appears that "x -match y -match z" is really actually only comparing x and y, so I get nothing. Which is obviously incorrect.

Can anyone explain how I compare and look for ANY duplicate amongst several variables?

Thanks

To further elaborate: To get my variables I'm doing this:

$SAMAccountList=gc "C:\My Documents\Domain2SAMList.txt"

$SamAccountList | % {
$one=Get-QADUser -SamAccountName $_ -IncludedProperties SamAccountName -Server dc.domain1.local | foreach { $_.SAMAccountName }
$oneNT=Get-QADUser -SamAccountName $_ -IncludedProperties NTAccountName -Service dc.domain1.local | foreach { $_.NTAccountName }
$twoSAM=Get-QADUser -SamAccountName $_ -IncludedProperties SamAccountName -Service dc.domain2.local | foreach { $_.SAMAccountName }
$twoNT=Get-QADUser -SamAccountName $_ -IncludedProperties NTAccountName -Service dc.domain2.local | foreach { $_.NTAccountName }
#...
#...
#...etc for each domain
}

This then gives me a set of variables as I mentioned above

1 Answer 1

1

I would put them in a customobject and search with the unique names.

$NAMES = @()

$NAMES  += [pscustomobject] @{name=$one;nameNT=$oneNT}
$NAMES  += [pscustomobject] @{name=$two;nameNT=$twoNT}
$NAMES  += [pscustomobject] @{name=$three;nameNT=$threeNT}
$NAMES  += [pscustomobject] @{name=$four;nameNT=$fourNT}
$NAMES  += [pscustomobject] @{name=$five;nameNT=$fiveNT}
$NAMES  += [pscustomobject] @{name=$six;nameNT=$sixNT}

$UniqueNames=($NAMES  | Sort-Object -Property name -Unique).Name

ForEach($Uniquename in $UniqueNames){

  if($Uniquename -ne $NULL){

     $SEARCH_RESULT=$NAMES|?{$_.Name -eq $Uniquename}

     if ( $SEARCH_RESULT.Count -gt 1)
     {     
            $SEARCH_RESULT
     }
  }
}

I get the following results

First data set
name         nameNT
----         ------
MSmith01     DOMAIN1\MSmith01
MSmith01     DOMAIN2\MSmith01
MSmith01     DOMAIN5\MSmith01

Second data set
name         nameNT
----         ------
MSmith01     DOMAIN2\MSmith01
MSmith01     DOMAIN3\MSmith01
Sign up to request clarification or add additional context in comments.

4 Comments

Hi hysh, Thanks for the response. Much appreciated. I'm going to expand on my original question to try to help myself and help you help me I hope!
I don't quite understand the comment about 'unique' either - as really I'm going to have a list which is either several $null variables, or several of the same SAM names. If I user a unique parameter, wouldn't I get issues with several of the variables also being $null?
HI I updated the reply with the results. If you give me the patterns of data that you are concerned about, I might be able to help you.
Hi hysh, I didn't quite understand the syntax, but, I tried and tested your suggestion and it works perfectly as is. Many thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.