This script does not make the new address the employee's primary SMTP address. In order to do this, you would need to remove the current primary denoted by the "SMTP:" at the start of the proxyAddress and return it back with "smtp:" then add the new e-mail address with "SMTP:" at the start. I will provide a code sample in the future that will detail that swap out and how to do it safely.
And remember, this script modifies data! Use at your own risk! I might have the best of intentions but my skill may betray you. Test, test and further test before implementing this code in a production environment.
Function Get-LocalDomainController($objectDomain) { return ([System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite()).Servers | Where-Object { $_.Domain.Name -eq $objectDomain } | ForEach-Object { $_.Name } | Select-Object -first 1 } Function Get-ObjectADDomain($distinguishedName) { return ((($distinguishedName -replace "(.*?)DC=(.*)",'$2') -replace "DC=","") -replace ",",".") } Function Get-ActiveDirectoryObject($distinguishedName) { return [ADSI]("LDAP://" + (Get-LocalDomainController (Get-ObjectADDomain $distinguishedName)) + "/" + ($distinguishedName -replace "/","\/")) } #--------------------------------------------------------------------------------------------------# Set-Variable -name forestRootDn -option Constant -value ([ADSI]("LDAP://" + (([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()).name) + "/rootDSE")).defaultNamingContext Set-Variable -name adsPropertyAppend -option Constant -value 3 Set-Variable -name newMailDomain -option Constant -value "@newdomain.local" Set-Variable -name inputFile -option Constant -value "userlist.txt" Set-Variable -name writeEnabled -option Constant -value $false #--------------------------------------------------------------------------------------------------# if(Test-Path -path $inputFile) { $userList = Get-Content -path $inputFile } else { Write-Host "Could not locate $inputFile. Exiting..." -foregroundColor Red exit } if(!(Test-Path -path "backup")) { New-Item -path "backup" -type directory | Out-Null } $objectConnection = New-Object -comObject "ADODB.Connection" $objectCommand = New-Object -comObject "ADODB.Command" $objectConnection.Open("Provider=ADsDSOObject;") $objectCommand.ActiveConnection = $objectConnection foreach($user in $userList) { $ldapBase = "GC://$forestRootDn" $ldapAttr = "distinguishedName" $ldapScope = "subtree" $ldapFilter = "(&(objectClass=user)(proxyAddresses=smtp:$user))" $ldapQuery = "<$ldapBase>;$ldapFilter;$ldapAttr;$ldapScope" $objectCommand.CommandText = $ldapQuery $objectRecordSet = $objectCommand.Execute() if(!$objectRecordSet.EOF) { while(!$objectRecordSet.EOF) { $userObject = Get-ActiveDirectoryObject $objectRecordSet.Fields.Item('distinguishedName').Value $newEmailAddress = ("smtp:" + ($user.Split("@")[0]).ToLower() + $newMailDomain) Write-Output ($userObject.displayName).ToString() Write-Output "New Address: $newEmailAddress" $notFound = $true Set-Content -path ("backup\" + ($userObject.mail).ToString() + ".txt") -value $userObject.proxyAddresses foreach($proxyAddress in $userObject.proxyAddresses) { if($proxyAddress -eq $newEmailAddress) { $notFound = $false } } if($notFound -eq $true) { Write-Output "Adding $newEmailAddress" if($writeEnabled -eq $true) { $userObject.PutEx($adsPropertyAppend, "proxyAddresses", @($newEmailAddress)) $userObject.SetInfo() } } else { Write-Output "Already has $newEmailAddress" } $objectRecordSet.MoveNext() } } else { Write-Output "Could not locate $user in the forest." } Write-Output ("-" * 50) }
No comments:
Post a Comment