Set Forwarding in Office 365 from CSV file

Introduction

This script is a variant from my previous post. It will allow you to set the forwarding property on a mailbox list, specified in a CSV file. The principle remains the same.

Inputs

  1. Exchange Online Admin credentials.
  2. A log file path, which will contains the potentials errors during the script execution, but also the succeful actions.
  3. A CSV file using ; as delimiter. The CSV file needs to looks like the following : 

 

The first colums must contains the User ID or user principal name (UPN), the second column must containsa valid email adress where the mails will be redirect.

The third column must contains the DeliverToMailboxAndForward parameter which specifies the message delivery behavior when a forwarding address is specified by the ForwardingAddress or ForwardingSmtpAddress parameters. Valid values are:

The default value is $false. The value of this parameter is meaningful only if you configure a forwarding recipient or email address.

Source : https://technet.microsoft.com/en-us/library/bb123981(v=exchg.160).aspx

Usage

  1. Open Windows PowerShell
  2. Run the Set-Forwarding.ps1
  3. Enter the Credential which is at least Exchange Online Administrator, then click OK.
  4. Enter your log file path, and your CSV forwarding list :
  5. Then click Enter to start the script.
  6. You can then check the results by using the Get-MailBox upn | Select ForwardingSmtpAddress, DeliverToMailboxAndForward

 

Ouput

The log file, that you will specified will contains all the actions made by the script.

Output Exemple 1

 

In this exemple, i have specified a bad file path or name for my CSV file.

Output Exemple 2

 

In this exemple, The log file informs you that the forwarding for the specified mailbox was successfully configured.

 

The Script :

PowerShell
Edit|Remove
#Method 1 to Get Credentials (Can be useful if you do not want to enter the credentials each time) 
#$powerUser = "User@domainO365" 
#$powerPass = "passaword" 
#$password = ConvertTo-SecureString $powerPass -AsPlainText -Force 
#$adminCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $powerUser,$password 
 
#Method 2 to get credentials 
$adminCredential = Get-Credential 
 
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $adminCredential -Authentication Basic -AllowRedirection 
Import-PSSession $Session 
Connect-MsolService -Credential $adminCredential 
$error.Clear() 
 
$MyLogFile = Read-host "Please enter the path of the log file" 
if([string]::IsNullOrEmpty($MyLogFile)) 
{ 
    $MyLogFile = "C:\Logs\SetForwarding.log" 
} 
 
Add-content –path $MyLogFile "#---------------------------------------------------------- " 
Add-content –path $MyLogFile "      Set mailbox forwarding in Office 365       " 
Add-content –path $MyLogFile "#---------------------------------------------------------- " 
Add-content –path $MyLogFile "" 
Add-content –path $MyLogFile "Start date and time $(Get-Date)" 
 
#Import source file 
$CSVfile = Read-host "Please enter the path of the CSV file which contains your forwarding list" 
$list = import-csv -path $CSVfile -delimiter ';' -ErrorAction Stop 
 
if ($Error.Count -ne 0){ 
    $MyDate = (get-date -uformat "%Y%m%d-%H.%M.%S").ToString() 
    $MyValueToWrite = "$MyDate - An error was thrown during the CSV import ($path)" 
    Write-Host "$MyValueToWrite" -ForegroundColor Red 
    Add-content –path $MyLogFile "$MyValueToWrite" 
    Add-content –path $MyLogFile $Error[0].Exception 
    $Error.Clear() 
} 
else { 
    foreach ($user in $list) { 
     
        #Set Forwarding SMTP adress in Office 365 
    Try{ 
        If($user.DeliverToMailboxAndForward -eq "true") 
        { 
            Set-Mailbox $user.upn -ForwardingSmtpAddress $user.ForwardingSmtpAddress -DeliverToMailboxAndForward $true 
        }else{ 
            Set-Mailbox $user.upn -ForwardingSmtpAddress $user.ForwardingSmtpAddress -DeliverToMailboxAndForward $false 
        } 
        if ($Error.Count -ne 0){ 
            $MyDate = (get-date -uformat "%Y%m%d-%H.%M.%S").ToString() 
                    $MyValueToWrite = "$MyDate - An error was thrown during the set of the forwarding address for :  $($user.upn)" 
                    Write-Host "$MyValueToWrite" -ForegroundColor Red 
                    $Error[0].Exception 
                    Add-content –path $MyLogFile "$MyValueToWrite" 
                    Write-Host "Erreur" 
                    Add-content –path $MyLogFile $Error[0].Exception 
            $Error.clear() 
        }else{ 
            #Log Informations into the log file 
            $MyDate = (get-date -uformat "%Y%m%d-%H.%M.%S").ToString() 
                    $MyValueToWrite = "$MyDate - The forwarding address has been set for :  $($user.upn) " 
                    Write-Host "$MyValueToWrite" -ForegroundColor Green 
                    Add-content –path $MyLogFile "$MyValueToWrite" 
                    Write-Host "" 
        } 
    } 
    Catch 
    { 
            $MyDate = (get-date -uformat "%Y%m%d-%H.%M.%S").ToString() 
                $MyValueToWrite = "$MyDate - An error was thrown during the set of the forwarding address for :  $($user.upn)" 
                Write-Host "$MyValueToWrite" -ForegroundColor Red 
                $Error[0].Exception 
                Add-content –path $MyLogFile "$MyValueToWrite" 
                Write-Host "" 
                Add-content –path $MyLogFile $Error[0].Exception 
        $Error.clear() 
    } 
    } 
}

Hoping that script will be useful for some of you.