PowerShell Script to generate report on ForwardTo and RedirectTo rules in Office365 and Exchange Server.

Below script generates reports on External forwarding and Redirect Rules setup on the mailboxes, this can be helpful where organization wants to review External forwarding emails address and domains, and wants to block or allow forwarding to those External domains.

Further we can use the Remote Domain to block or allow the forwarding.


#====================================================
# Get-InboxRule-External-and-RedirectTo.ps1
# Author: Sunil Chauhan
# Email:Sunilkms@gmail.com
# website:sunil-Chauhan.blogspot.com
# This scripts gets all the forwarding and redirectTo Rules and `
# Create a Report in a presentable format.
#====================================================

Write-host "Getting All User Mailboxes.."
$AllMailboxes = Get-Mailbox -resultSize Unlimited

$AllRules = @()
$c=0
foreach ($mbx in $allMailboxes)
{
$c++
Write-host "$C : Getting Rules For User Mailbox:" $mbx.Alias
$mbxRules= Get-InboxRule -Mailbox $mbx.Alias
$AllRules+=$MbxRules
}

$Rules = $AllRules | ? {$_.Description -match "@" -and $_.ForwardTo -ne $null -or $_.RedirectTo -ne $null}

$RulesDATA=@()
foreach ($rule in $Rules)
{
if ($rule.ForwardTo)
{
if (($rule.ForwardTo).Count -gt 1)
{
foreach ($entry in $rule.ForwardTo)
{
if ($entry -match "@"){
$RulesD= New-Object -TypeName PSObject
$RulesD| Add-Member -MemberType NoteProperty -Name Mailbox -Value $rule.MailboxOwnerID
$RulesD| Add-Member -MemberType NoteProperty -Name ForwardTo -Value $($entry | % {$($_.split("[")[0]).Replace('"',"")})
#$RulesD| Add-Member -MemberType NoteProperty -Name ForwardTo -Value $entry
$RulesD| Add-Member -MemberType NoteProperty -Name RedirectTo -Value "n/a"
$RulesDATA+=$rulesD}
}
}
Else{
if ($rule.ForwardTo -match "@"){
$RulesD= New-Object -TypeName PSObject
$RulesD| Add-Member -MemberType NoteProperty -Name Mailbox -Value $rule.MailboxOwnerID
$RulesD| Add-Member -MemberType NoteProperty -Name ForwardTo -Value $($rule.ForwardTo | % {$($_.split("[")[0]).Replace('"',"")})
$RulesD| Add-Member -MemberType NoteProperty -Name RedirectTo -Value "n/a"
$RulesDATA+=$rulesD}
}
}

if ($rule.RedirectTo)
{
if (($rule.RedirectTo).Count -gt 1)
{
foreach ($entry in $rule.RedirectTo)
{
if ($entry -match "@") {
$RulesD= New-Object -TypeName PSObject
$RulesD| Add-Member -MemberType NoteProperty -Name Mailbox -Value $rule.MailboxOwnerID
$RulesD| Add-Member -MemberType NoteProperty -Name ForwardTo -Value n/a
$RulesD| Add-Member -MemberType NoteProperty -Name RedirectTo -Value $($entry | % {$($_.split("[")[0]).Replace('"',"")})
$RulesDATA+=$rulesD}
}
}
Else{
if ($rule.RedirectTo -match "@") {
$RulesD= New-Object -TypeName PSObject
$RulesD| Add-Member -MemberType NoteProperty -Name Mailbox -Value $rule.MailboxOwnerID
$RulesD| Add-Member -MemberType NoteProperty -Name ForwardTo -Value "N/A"
$RulesD| Add-Member -MemberType NoteProperty -Name RedirectTo -Value $($rule.RedirectTo | % {$($_.split("[")[0]).Replace('"',"")})
$RulesDATA+=$rulesD }
}
}
}

$RulesDATA | Export-Csv "Forwarding-Rules-Report.csv" -notype

Comments