# FindDuplIDs.ps1 # Script to find objects with duplicates among the following AD attributes: # userPrincipalName, mail, msRTCSIP-PrimaryUserAddress, proxyAddresses # # Copyright (c) 2018-2019 Richard L. Mueller # Version 1.0 - December 8, 2018 # Version 1.1 - March 1, 2019 # Quote attribute with lDAPDisplayName msRTCSIP-PrimaryUserAddress. # # ---------------------------------------------------------------------- # You have a royalty-free right to use, modify, reproduce, and # distribute this script file in any way you find useful, provided that # you agree that the copyright owner above has no warranty, obligations, # or liability for such use. # Retrieve all objects where any of the attributes are assigned values. $Prop = @("userPrincipalName","mail","msRTCSIP-PrimaryUserAddress","proxyAddresses") $Filter = "(|(userPrincipalName=*)(mail=*)(msRTCSIP-PrimaryUserAddress=*)(proxyAddresses=*))" $Objects = Get-ADObject -LDAPFilter $Filter -Properties $Prop # Hash table of IDs. The key is the ID (the value of one of the 4 attributes), # the value is the DN of the objects with the value (and the attribute names). # The DNs in the value are separated by the "@" character. It is assumed that # no distinguished names have this character. $IDs = @{} # Loop through the objects. ForEach ($Object In $Objects) { # Retrieve attribute values. $DN = $Object.distinguishedName $UPN = $Object.userPrincipalName $Mail = $Object.mail $PrimAddr = $Object."msRTCSIP-PrimaryUserAddress" $ProxyAddrs = $Object.proxyAddresses # Check userPrincipalName. If ($UPN) { # Check if this ID has been seen already. If ($IDs.ContainsKey($UPN)) { # Duplicate ID, append to value. $IDs[$UPN] = $IDs[$UPN] + "@$DN `(UPN)" } Else { # Add this ID to the hash table. $IDs.Add($UPN, "$DN `(UPN)") } } # Check mail. If ($Mail) { # Only consider value after any colon character. $Mail = ($Mail.Split(":"))[-1] # Check if this ID has been seen already. If ($IDs.ContainsKey($Mail)) { # Check for the current DN. If ($IDs[$Mail] -Like "*$DN `(*") { # Add mail to the list of attributes for this DN. $IDs[$Mail] = $IDs[$Mail].Replace("$DN `(","$DN `(mail,") } Else { # Duplicate ID, append to value. $IDs[$Mail] = $IDs[$Mail] + "@$DN `(Mail)" } } Else { # Add this ID to the hash table. $IDs.Add($Mail, "$DN `(mail)") } } # Check msRTCSIP-PrimaryUserAddress. If ($PrimAddr) { # Only consider value after any colon character. $PrimAddr = ($PrimAddr.Split(":"))[-1] # Check if this ID has been seen already. If ($IDs.ContainsKey($PrimAddr)) { # Check for the current DN. If ($IDs[$PrimAddr] -Like "*$DN `(*") { # Add msRTCSIP-PrimaryUserAddress to the list of attributes for this DN. $IDs[$PrimAddr] = $IDs[$PrimAddr].Replace("$DN `(","$DN `(msRTCSIP-PrimaryUserAddress,") } Else { # Duplicate ID, append to value. $IDs[$PrimAddr] = $IDs[$PrimAddr] + "@$DN `(msRTCSIP-PrimaryUserAddress)" } } Else { # Add this ID to the hash table. $IDs.Add($PrimAddr, "$DN `(msRTCSIP-PrimaryUserAddress)") } } # Check proxyAddresses. If ($ProxyAddrs) { # Check each address in proxyAddresses. ForEach ($Addr In $ProxyAddrs) { # Only consider value after any colon character. $Addr = ($Addr.Split(":"))[-1] # Check if this ID has been seen already. If ($IDs.ContainsKey($Addr)) { # Check for the current DN. If ($IDs[$Addr] -Like "*$DN `(*") { # Add proxyAddresses to the list of attributes for this DN. $IDs[$Addr] = $IDs[$Addr].Replace("$DN `(","$DN `(proxyAddresses,") } Else { # Duplicate ID, append to value. $IDs[$Addr] = $IDs[$Addr] + "@$DN `(proxyAddresses)" } } Else { # Add this ID to the hash table. $IDs.Add($Addr, "$DN `(proxyAddresses)") } } } } # Enumerate all IDs. ForEach ($ID In $IDs.Keys) { $Values = $IDs[$ID].Split("@") If ($Values.Count -gt 1) { "Duplicate ID: $ID" ForEach ($Entry In $Values) { " $Entry" } } }