Sunday, November 20, 2016

ADD additional SMTP Email Address Exchange Hybrid

When you run Hybrid Wizard for setting up Exchange onpremise to Exchange online integration, one of the step --> updates the email address policy to include

alias@domain.mail.microsoft.com.

 This adds additional email address to all the users that have Automatically update Email adddresses based on e-mail address policy is checked.



There are always mailboxes in the enviornment where this policy is set to false for reasons.

This nice litte script will update those mailboxes with the required email adddress along with logging & reporting.

Download & extract the zip file from below link , update the .ps1 :

https://gallery.technet.microsoft.com/scriptcenter/ADD-additional-SMTP-b3c3a055



#update the values as per your enviornment

$hybemaildom = "@domain.mail.onmicrosoft.com" 

$email1 = "VikasS@labtest.com"

$from = "EolemailFix@labtest.com"

$smtpserver = "smtpserver"

Run the batch file or run the .ps1 in the exchange shell. Execution scope is only the mailboxes that have email address policy set to false.

CSV report will be sent on email address specified & will also be saved in report folder.



Note: Script has been tested with exchange2010 but should work with other versions as well.


<#     
    .NOTES 
    =========================================================================== 
     Created on:       10/19/2016 12:30 PM 
     Created by:       Diwakar Sharma 
          Reviewed by:   Vikas Sukhija (http://msexchange.me) 
     Organization:      
     Filename:   Additional SMTP o365       
    =========================================================================== 
    .DESCRIPTION 
        This script will be used to add additional SMTP addtess for o365 hybrid setup. 
        There are situation where Email policy is uncheked so this script can be utilized. 
#>
 
################Add Snapin & Logs/ variables######################### 
$Error.clear() 
If ((Get-PSSnapin | where { $_.Name -match "Microsoft.Exchange.Management.PowerShell.E2010" }) -eq $null

    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 

$date1 = get-date -format d 
$date1 = $date1.ToString().Replace("/""-"
$time = get-date -format t 
 
$time = $time.ToString().Replace(":""-"
$time = $time.ToString().Replace(" """
 
$log = ".\logs" + "\" + "Ps_log_" + $date1 + "_" + $time + "_.log" 
$report1 = (Get-Location).path + "\" + "Report" + "\" + "Report_Hybemail" + $date1 + "_" + $time + "_.csv" 
 
$hybemaildom = "@domain.mail.onmicrosoft.com" 
$collection = @() 
 
$email1 = "VikasS@labtest.com" 
$from = "EolemailFix@labtest.com" 
$smtpserver = "smtpserver" 
 
Start-Transcript -Path $log 
 
#############get all mailboxes where policy is not applied##### 
 
$AliasAll = Get-Mailbox -ResultSize Unlimited | Where { $_.EmailAddressPolicyEnabled -ne "True" } | select Alias,emailaddresses 
$Error.clear() 
Foreach ($Als in $AliasAll

    Write-Host "Processing ..............."$Als.alias"" -ForegroundColor green 
    $mcoll = "" | select Alias, hybemail, Status 
    $AddSMTP = $Als.Alias + $hybemaildom 
    $mcoll.Alias = $Als.Alias 
    $mcoll.hybemail = $AddSMTP 
    $AddSMTP 
    $alladdreses = $Als.EmailAddresses | select -ExpandProperty addressstring 
    if($alladdreses -contains $AddSMTP){ 
    $mcoll.Status = "already Present" 
    } 
    else
    $Error.clear() 
    Set-Mailbox $Als.Alias -EmailAddresses @{ add = $AddSMTP } 
    if ($error
    { 
        $mcoll.Status = "Error" 
        $Error.clear() 
    } 
    else 
    { 
        $mcoll.Status = "Success" 
    }} 
    $collection+=$mcoll 

$collection | Export-Csv $report1 -NoTypeInformation 
#############Send Report############################## 
$msg = new-object Net.Mail.MailMessage 
$smtp = new-object Net.Mail.SmtpClient($smtpServer
$msg.From = $from 
$attach = new-object Net.Mail.Attachment($report1
$msg.To.Add($email1
$msg.Subject = "Exchange Online Emal Address Fix Report" 
$msg.Body = "Exchange Online Emal Address Fix Report" 
$msg.Attachments.Add($attach
$smtp.Send($msg
get-date 
Stop-Transcript 
################################################################




Regards

Sukhija Vikas

http://msexchange.me

5 comments:

  1. Hi
    i think you have a mistake here:
    if($alladdreses -contains $AddSMTP)

    needs to be:
    if($alladdreses -match $AddSMTP)

    Thanks

    ReplyDelete
  2. No contains is working fine..I just retested it as well ,where as match is failing , i just removed one letter from end & it still shows true as it tries to match not contain...

    [PS] C:\Windows\system32>if($alladdreses -contains $AddSMTP){"True"} else{"false"}
    True
    [PS] C:\Windows\system32>if($alladdreses -match $AddSMTP){"True"} else{"false"}
    True
    [PS] C:\Windows\system32>$AddSMTP = $Als.Alias + "@xxxx.mail.onmicrosoft.co"
    [PS] C:\Windows\system32>if($alladdreses -match $AddSMTP){"True"} else{"false"}
    True
    [PS] C:\Windows\system32>if($alladdreses -contains $AddSMTP){"True"} else{"false"}
    false

    ReplyDelete
  3. Thanks
    weird, i tested and didn't seem to work

    ReplyDelete
  4. ok i figured it out.
    its good to find the alias@...onmicrosoft
    but if the user has already onmicrosoft(not with alias) it wont find it
    in that case -match will be more accurate since it doesn't care what kind of address the user has as long as its @tenant.mail.onmicrosoft.com that means it already has it.

    ReplyDelete
  5. Yes script was written for alias as thats what is set by Hybrid wizard but its good if anyone want to figure out onmicrosoft(not with alias) than he can refer your comments in this post :)

    ReplyDelete