######################################################################################################################################################################### ######################################################################################################################################################################### ## ## Merge Contact Script ## ============================================ ## ## ## Purpose: ## ## Merging the contact into the MEU created by PrepareMoveRequest.PS1 script ## ## ## Funtionality Overview: ## ## a) Search for an existing contact in the forest in the FIM Sync OU. ## b) If a contact already exists then merge the LegacyExchangeDN value of the Mail Contact in the existing MEU created by PrepareMoveRequest.ps1 and delete the contact. ## ########################################################################################################################################################################### ########################################################################################################################################################################### $Date=Get-Date $Logfilepath= "logs\ProgressLog_{0}{1:d2}{2:d2}-{3:d2}{4:d2}{5:d2}" -f $date.year,$date.month,$date.day,$date.hour,$date.minute,$date.second + ".log" $DirectoryServer1= "Root Domain DC name" $DirectoryServer2= "Child Domain DC" $FIMSyncOU= "OU Name" ################################################################################################################################ # # Function: Logging # ======= # # Input Parameters: # $logtext: The text string to be written to the console or to a log file # $logtype: The type of message to be written: normal, error, or debug # # Purpose: # - Writes a text string to the console and/or to log files depending on the type. # type=cINFO: output goes to console and the normal log file # type=cERROR: Output goes to Console, normal log file, and error log file # # # ############################################################################################################################### function logger( $logtext, $logtype ) { switch($logtype) { cINFO { write-host $logtext -b black -f green $logtext | Out-file $Logfilepath -append } cERROR { write-host "ERROR: " $logtext -b black -f red "ERROR: " + $logtext | Out-file $logfilepath -append } csv { write-host $logtext -b black -f green $logtext | Out-file $csvfilepath -append } } } ## ----------------------------------------- End of Function "Logging" --------------------------------------------------------- ######################################################################################################################### # # Function: UpdateSMTPforMailbox # =========================== # # # # Purpose: # - Adds LegacyExchangeDN of the contact as X500 addresses of the Mail User # # ######################################################################################################################### function UpdateSMTPforMailbox($LDN, $useralias) { Get-MailUser -identity:$useralias -domaincontroller:$DirectoryServer2 | foreach-object { $MailboxID = $_."Identity" } $newx500= "X500:" + $LDN $error.clear() Set-MailUser -domaincontroller:$DirectoryServer2 -Identity:$MailboxID -EmailAddresses:((Get-MailUser -domaincontroller:$DirectoryServer2 -Identity:$MailboxID).EmailAddresses + $newx500 ) if($error.count -gt 0) { logger ( "The LegacyExchangeDN Addresses couldn't be added : " + $newx500 + " : " + $error + " : " + "$useralias" ) cERROR } else { logger (" The LegacyExchangeDN has been added successfully : " + $newx500 + ": " + "$useralias") CINFO } } } ## ----------------------------- End of Function "UpdateSMTPforMailbox"----------------------------------## ##################### Script Start ################################### import-csv -path "C:\Migration\Mailbox.csv" | foreach-object { $useralias=$_."Identity" $error.clear() Get-MailContact -identity:$useralias -DomainController:$DirectoryServer1 -OrganizationalUnit:$FIMSyncOU | Foreach-object { $LDN = $_.LegacyExchangeDN $DN = $_.distinguishedName $name = $_.name Write-host "LegacyExchangeDN :" $LDN Write-host "Distinguished Name :" $DN Write-host "Name :" $name $newname = "CN=" + $name Write-host "Updated Display Name :" $newname } if($error.count -gt 0) { logger ( "ERROR GETTING THE MAIL CONTACT: " + $useralias + " : " + $error ) cERROR } else { logger (" MAIL CONTACT DN OBTAINED SUCCESSFULLY: " + $useralias + " : " + $LDN) CINFO $error.clear() ## Remove mail contact by LDAP connection as it is created by FIM ## $Class = “Contact” $strUsrName = “$newname” $objADSI = [ADSI]“LDAP://$FIMSyncOU” $objUser = $objADSI.Delete($Class, $strUsrName) #$objUser.setInfo if($error.count -gt 0) { logger ( "ERROR REMOVING MAIL CONTACT : " + $useralias + " : " + $error ) cERROR } else { logger (" MAIL CONTACT REMOVED SUCCESSFULLY " + $useralias + " : " + $LDN) CINFO Start-Sleep -s 60 UpdateSMTPforMailbox ($LDN) ($useralias) } Logger ("_____________________________________________________________________________________________________") CINFO } } ##################### Script End ######################################