Thursday, November 22, 2007

Script to find and fix mismatches between the Reply Address and Active Directory Mail property

The following script is an ADSI powershell script that can be used check and update any user accounts where you have a mismatch between the mail property in Active Directory and the Reply address that has been set for a users mailbox. If you’re running Exchange 2007 you might want to look at using Set-Mailbox –ApplyMandatoryProperties instead.

This script is pretty simple it will look at any mail-enabled users and check the active directory mail attribute against the Identified Reply address. If any mismatches are found it will then prompt to ask if the user wants to update the Active Directory Mail property to be the same as the Reply Address. To guard against null address if a null property is founds the script will just echo out the account that is have a null property.

I’ve created two version of the script the first just uses ADSI so it will work on any version of Exchange and will to scan the current domain the executing workstation is in the second uses get-mailbox in Exchange 2007 which may be more helpful if you have a multi-domain forest (this will only work with the Exchange Management Shell).

I’ve put a download of this script here the code itself looks like

$root = [ADSI]'LDAP://RootDSE'
$dfDefaultRootPath = "LDAP://" + $root.DefaultNamingContext.tostring()
$dfRoot = [ADSI]$dfDefaultRootPath
$gfGALQueryFilter = "(&(&(&(& (mailnickname=*)(objectCategory=person)(objectClass=user)(msExchHomeServerName=*)))))"
$dfsearcher = new-object System.DirectoryServices.DirectorySearcher($dfRoot)
$dfsearcher.PageSize = 900
$dfsearcher.Filter = $gfGALQueryFilter
$srSearchResult = $dfsearcher.FindAll()
$mbcount = 0
$upall = 0
$skippall = 0
foreach ($emResult in $srSearchResult) {
$mbcount++
if (($mbcount % 100) -eq 0 ) {$mbcount.ToString() + " Mailboxes Processed"}
$uoUserobject = New-Object System.DirectoryServices.directoryentry
$uoUserobject = $emResult.GetDirectoryEntry()
$raReplayAddress = ""
foreach($maMailAddress in $uoUserobject.Proxyaddresses){
if ($maMailAddress.indexofany("SMTP:") -eq 0){
$raReplayAddress = $maMailAddress.ToString().Replace("SMTP:","")
}
}
if ($uoUserobject.mail.value -ne $null -band $raReplayAddress -ne ""){
if($uoUserobject.mail.Value.ToLower() -ne $raReplayAddress.ToLower()){
"MissMatch " + $uoUserobject.mail.Value.ToLower() + " " + $raReplayAddress.ToLower()
if ($upall -eq 0 -band $skippall -eq 0){
$answer = Read-Host "Do you want to modify this Object [Y] Yes [A] Yes to All [N] No [L] No to all "
switch ($Answer)
{
"Y" { if ($raReplayAddress -ne ""){
$uoUserobject.mail.Value = $raReplayAddress
$uoUserobject.SetInfo()
"Address Updated"}
else {"Proxy Address Null !! not updating"}
}
"" { "Not updating"}
"A" { if ($raReplayAddress -ne ""){
$uoUserobject.mail.Value = $raReplayAddress
$uoUserobject.SetInfo()
$upall = 1
"Address Updated"}
else {"Proxy Address Null !! not updating"}
}
"N" {"Not updating" }
"L" {"Not updating"
$skippall = 1
}
}

}
else {if ($upall -eq 1 -band $skippall -eq 0){
if ($raReplayAddress -ne ""){
$uoUserobject.mail.Value = $raReplayAddress
$uoUserobject.SetInfo()
"Address Updated"}
else {"Proxy Address Null !! not updating"}
}
}
}
}
else{
if ($uoUserobject.mail.value -eq $null){"**** Null Ad Mail Property : " + $uoUserobject.name}
if ($raReplayAddress -eq ""){"***** Null Proxyaddress : " + $uoUserobject.name}

}
}

"Total number of Mailboxes Processed :" + $mbcount


No comments: