Install ADFS on Azure VM step by step

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 the 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

  1. Hi,
    found this script and tried it on our Exchange 2016, but after a few users I get an error.
    Do you have an updated version of this script?

    Error is,
    "Method invocation failed because [Microsoft.Exchange.Data.Storage.Management.ADRecipientOrAddress] does not contain a m
    ethod named 'split'.
    At C:\temp\forwards.ps1:54 char:86
    + ... ($entry | % {$($_.split("[")[0]).Replace('"',"")})
    + ~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

    You cannot call a method on a null-valued expression.
    At C:\temp\forwards.ps1:54 char:86
    + ... ($entry | % {$($_.split("[")[0]).Replace('"',"")})
    + ~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
    "

    ReplyDelete
    Replies
    1. Hi, It is probably some data stream change in object value $entry on line 54, if you know some scripting you can fix the data processing in this line, ($entry | % {$($_.split("[")[0]).Replace('"',"")}). Or else will try to create setup an environment to test and fix it.

      Delete

Post a Comment