Mailbox Forwarding on Office 365

New function for today, forward and cancel mailbox forwarding on Office 365 as part of the 365 Powershell Administration System that I have written.

 

 

function global:fSetMailboxForwarding {
$xIdentity = fCollectIdentity -xText “Enter Alias of User to Forward:”
if ($xIdentity -eq $false) {Return $false}

$xMailbox = get-mailbox -identity $xIdentity

fDisplayInfo -xText “The current setup is:”
write-host ($xMailbox | select DisplayName, PrimarySMTPAddress, ForwardingAddress, ForwardingSmtpAddress, DeliverToMailboxAndForward | format-table | out-string)

if (($xMailbox.ForwardingAddress -ne $null) -OR ($xMailbox.ForwardingSMTPAddress -ne $null)    ) {

$xFwdCancel = fUserPrompt -xQuestion “Would you like to cancel the Current Forwarding? (y/n)”

if ($xFwdCancel -eq “y”) {
set-mailbox -identity $xIdentity -ForwardingAddress $null -ForwardingSMTPAddress $null -DeliverToMailboxAndForward $false
}elseif ($xFwdCancel -eq “n”) {
$xMailbox = get-mailbox -identity $xIdentity
fDisplayInfo -xText “Your configuration has not changed” -xColor Yellow
pause
Return $false
}else{
Return $false
}

} else {

$xFwdAddress = fCollectIdentity -xText “Who would you like to forward email to?”
if ($xFwdAddress -eq $false) {Return $false}

$xDelToMailbox = fUserPrompt -xQuestion “Would you like to continue delivering to the Mailbox? (y/n)”

if ($xDelToMailbox -eq “y”) {
set-mailbox -identity $xIdentity -ForwardingAddress $xFwdAddress -DeliverToMailboxAndForward $true
}elseif ($xDelToMailbox -eq “n”) {
set-mailbox -identity $xIdentity -ForwardingAddress $xFwdAddress -DeliverToMailboxAndForward $false
}else{
Return $false
}

}

$xMailbox = get-mailbox -identity $xIdentity
fDisplayInfo -xText “The New setup is:”
write-host ($xMailbox | select DisplayName, PrimarySMTPAddress, ForwardingAddress, ForwardingSmtpAddress, DeliverToMailboxAndForward | format-table | out-string)
pause
}