Reputation: 1177
Currently I have the script below, which will compare email addresses in the CSV to the main mail address in ActiveDirectory, but it does not take proxy addresses into account. For instance, if Mary Smith had an email address [email protected], then Mary got married and her last name changed to Jones. Her standard email address is still [email protected] but she now has a proxy called [email protected].
How do I use this script to also validate against the proxyaddresses? Preferably without a huge hit to Active Directory .
$path = "H:\users.csv"
$csv = Import-Csv $path
Import-Module ActiveDirectory
foreach ($line in $csv)
{
$User = Get-ADUser -LDAPFilter "(&(objectclass=user)(mail=$($line.Email)))"
if ($User -eq $Null) {"User does not exist in AD " + $line.Email }
else {"User found in AD - " + $line.Email}
}
Upvotes: 1
Views: 1380
Reputation: 12323
AD has a proxyAddresses attribute for mail-enabled users, which includes the primary SMTP address as well as any aliases. Change your -LDAPFilter argument to this:
"(&(objectclass=user)(proxyAddresses=*$($line.Email)*))"
BTW, it's possible for the mail attribute to differ from the primary SMTP address in proxyAddresses, because the latter is enforced by Exchange but the former can be changed at will outside Exchange. It's not likely if you have a single Exchange organization integrated with the domain, but if you're concerned about that possibility you can use this filter:
"(&(objectclass=user)(|(mail=$($line.Email))(proxyAddresses=*$($line.Email)*)))"
Upvotes: 1
Reputation: 68273
Assuming you're running Exchange, you'd be much better off using the Exchange Get-Recipient cmdlet for this. Exchange maintains a database indexed by SMTP address, so those lookups are immediate. AD does not, so it must search all the ProxyAddreses of every user looking for a match.
$ExSession = new-pssession -configurationname Microsoft.Exchange -ConnectionURI http://<ExchangeServerName>/powershell/ -authentication kerberos
foreach($line in $csv)
{
if (Invoke-Command {Get-Recipient $args[0]} -ArgumentList $line.Email -Session $ExSession -ErrorAction SilentlyContinue)
{"User exists in AD"}
else {"User not found in AD"}
}
Substitue the name of one of your Exchange servers for in the new-pssession command.
Upvotes: 1