To implement what you want, you can create a Business Rule triggered after updating a user. To pass the external forwarding address to the Business Rule, you can use an Adaxes virtual property that can store text values, for example, CustomAttributeText1. If the virtual property that you chose is modified and not empty, the Business Rule will enable forwarding to the specified address, otherwise, if the property was cleared, the Business Rule will disable forwarding.
-
Create a new Business Rule.
-
On the 2nd step of the Create Business Rule wizard, select User and After Updating a User.
-
On the 3rd step, add the Run a program or PowerShell script action and paste the following script in the Script field. This script enables forwarding to an external SMTP address.
$exchangeServer = "ExchangeServer.domain.com" # TODO: modify me
$smtpAddress = "%adm-CustomAttributeText1%" # TODO: modify me
# Create a remote PowerShell session to Exchange Server
$session = New-PSSession -configurationname Microsoft.Exchange -connectionURI http://$exchangeServer/PowerShell
Import-PSSession $session -DisableNameChecking -AllowClobber
# Enable forwarding
Set-Mailbox -Identity "%distinguishedName%" -DeliverToMailboxAndForward $true -ForwardingSMTPAddress $smtpAddress
# Close the remote session and free up resources
Remove-PSSession $session
-
In the script, modify the following to match your environment:
- $exchangeServer - specifies the fully qualified domain name (FQDN) of your Exchange Server.
- $smtpAddress - specifies a value reference for the property that will be used to pass the external address. For example, if you want to use CustomAttributeText1, specify %adm-CustomAttributeText1%.
-
Enter a short description for the script and click OK.
-
Now, you need to add a condition when the script will be executed. Right-click the action that you've just added and select Add Condition.
-
Select the If <property> <changed> condition type.
-
In the <property> drop-down list, select Show all properties and select the virtual property that you chose, e.g. CustomAttributeText1.
-
Select has changed.
-
Click OK.
-
Also, the script to enable forwarding should be launched only when a forwarding address is specified, that is, when the virtual property is not empty. Right-click the action that you've added and select Add Condition again.
-
Select the If <property> <relation> <value> condition type.
-
In the <property> drop-down list, select Show all properties and select the virtual property that you chose, e.g. CustomAttributeText1.
-
Select is not empty.
-
Click OK.
-
Now, you need to add an action to disable forwarding. Click the Add action to a new set link.
-
Select the Run a program or PowerShell script action and paste the following script in the Script field. This script disables forwarding to an external SMTP address.
$exchangeServer = "ExchangeServer.example.com" # TODO: modify me
# Create a remote PowerShell session to Exchange Server
$session = New-PSSession -configurationname Microsoft.Exchange -connectionURI http://$exchangeServer/PowerShell
Import-PSSession $session -DisableNameChecking -AllowClobber
# Disable forwarding
Set-Mailbox -Identity "%distinguishedName%" -DeliverToMailboxAndForward $true -ForwardingSMTPAddress $NULL
# Close the remote session and free up resources
Remove-PSSession $session
-
In the script, modify the following to match your environment:
- $exchangeServer - specifies the fully qualified domain name (FQDN) of your Exchange Server.
- $smtpAddress - specifies a value reference for the property that will be used to pass the external address.
-
Enter a short description for the script and click OK.
-
Now, you need to add a condition when the script will be executed. Right-click the action that you've just added and select Add Condition.
-
Select the If <property> <changed> condition type.
-
In the <property> drop-down list, select Show all properties and select the virtual property that you chose, e.g. CustomAttributeText1.
-
Select has changed.
-
Click OK.
-
Also, the script to enable forwarding should be launched only when a forwarding address is not specified, that is, when the virtual property is empty. Right-click the action that you've added and select Add Condition again.
-
Select the If <property> <relation> <value> condition type.
-
In the <property> drop-down list, select Show all properties and select the virtual property that you chose, e.g. CustomAttributeText1.
-
Select is empty.
-
Click OK. You should receive something like this:
-
Finish creation of the Business Rule.