Monday, June 15, 2015

Function to enable or disable email forward

The Need

A function to manage the fowarding address for an Exchange 2010. The function needs to be easily used in PowerShell Web Access (PSWA).
Need to do what's EMC can do here :

Delivery options EMC Exchange 2010
EMC - MailFlow Settings - Delivry Options

The Function

I build this function :

 .Synopsis  
   Connect a remote session to the Exchange 2010 server.  
   Enable or disable the Forwarding address to. Enable or not the option Deliver to both  
   forwarding address and mailbox.  
 .DESCRIPTION  
   This cmdlet need to have access to the cmdlet of Exchange.  
   Its script to be used with Exchange 2010.  
   Version 1.0  
   Script by Jeremie Lauzier - Network Administrator, 2015-06-03  
 .EXAMPLE  
  Forward all email from Bruce Wayne to Clark Kent. No copies sent to Bruce Wayne.  
  Set-XXXEmailTransfert -accountmodify wayneb -forwardTo kentc -copyboth False  
 .EXAMPLE  
  Forward all email from Bruce Wayne to Clark Kent. Copies sent to both.  
  Set-XXXEmailTransfert -accountmodify wayneb -forwardTo kentc -copyboth True  
 .EXAMPLE  
  Disable the forward from Bruce Wayne to Clark Kent.  
  Set-XXXEmailTransfert -accountmodify wayneb -forwardTo $null -copyboth False  
 #>  
 function Set-XXXEmailTransfert  
 {  
   [CmdletBinding()]  
   [OutputType([int])]  
   Param  
   (  
     # Define the account to modify  
     [Parameter(Mandatory=$true,  
           ValueFromPipelineByPropertyName=$true,  
           Position=0,  
                       HelpMessage="Enter the account to modify.")]  
     $accountmodify,  
     # Define the account who will receive emails  
     [Parameter(Mandatory=$true,  
                       HelpMessage="Enter account will received emails.")]  
     [AllowNull()]  
     $forwardTo,  
     # Send copy to both  
     [Parameter(Mandatory=$true,  
           HelpMessage="Enable or not if emails delevry to both.")]  
     [ValidateSet("True", "False")]  
     $copyboth  
   )  
   Begin  
   {  
   $exchsession=New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://yourserver/powershell  
   Import-PSSession $exchsession -AllowClobber  
   }  
   Process  
   {if ($copyboth -eq "True")  
    {  
     Set-Mailbox $accountmodify -ForwardingAddress $forwardTo -DeliverToMailboxAndForward $true   
    }  
       else  
    {  
        Set-Mailbox $accountmodify -ForwardingAddress $forwardTo -DeliverToMailboxAndForward $false  
    }  
    Get-Mailbox $accountmodify | Format-Table Name, ForwardingAddress, DeliverToMailboxAndForward -Wrap  
   }  
   End  
   {  
   Remove-PSSession $exchsession  
   }  
 }  

Feel free to comment.
J.

No comments:

Post a Comment