Jump to content

Recommended Posts

Posted (edited)

Wrote a PowerShell function to manage forwarding and inbox rules on mailboxes. (Needs some more work/ refinements - (many cats.....) but it works)

 

<#
.Synopsis
  Function that can check and remove a mailbox forwarding and inbox rule(s) in O365.
.ALIASES
   clrmbx
.DESCRIPTION
  This function can be used to check/ and remove mailbox forwarding settings.  The function can also export any inbox rules a user mailbox has setup 
  (the default location is (C:\localapp\$($identity)-ExpIbxRules.csv) along with also being able to remove these rules.  A connection to O365 with the
  correct permissions is required.
.EXAMPLE
  Clean-MbxRules -Identity UserName -CheckSMTPFwdSettings
.EXAMPLE
  Clean-MbxRules -Identity UserName -RemoveSMTPFwdSettings
.EXAMPLE
   Clean-MbxRules -Identity UserName -CheckInBoxRules -ExportInBoxRules -OutFileLocation "C:\temp\UserName-IbxRules.csv"
.EXAMPLE
   Clean-MbxRules -Identity UserName -CheckInBoxRules -ExportInBoxRules -OutFileLocation "C:\temp\UserName-IbxRules.csv" -RemoveInBoxRules            
.OUTPUTS
  Outputs Inbox rules when the -ExportInBoxRules switch is used, the default location C:\localapp\$($identity)-ExpIbxRules.csv this location
  can be modified by using the -OutFileLocation switch.
.NOTES
   Author: H.A Plumtree
   Date: 04/07/2010
   Version: 1.0.0

   Pulls the functionality of the get-mailbox/ set-mailbox cmdlets in to one funtion.  This function can then be triggered to run when
   AD accounts are Disabled/ Deleted from AD to clean up the mailbox in O365
   
   Backlog/ Changes:
   
#>

function Clean-MbxRules
{

   param(
       [Parameter(Mandatory = $true)]$Identity,
       [Parameter(Mandatory = $false)]$OutFileLocation = "C:\localapp\$($identity)-ExpIbxRules.csv",
       [switch]$CheckSMTPFwdSettings,
       [switch]$RemoveSMTPFwdSettings,
       [switch]$CheckForwardingAddress,
       [switch]$RemoveForwardingAddress,
       [switch]$CheckDeliverToMailboxAndForward,
       [switch]$DisableDeliverToMailboxAndForward,
       [switch]$CheckInBoxRules,
       [switch]$ExportInBoxRules,
       [switch]$RemoveInBoxRules
        )

   $Identity = Get-Mailbox -Identity $identity | % {
   
       if($checkSMTPFwdSettings.IsPresent){
   
           if($_.ForwardingSmtpAddress -eq $null){
               write-host -fore green "No SMTP forwarding address set on account $($_.UserPrincipalName)"
               }
               else{
                   Write-Host -fore Yellow "SMTP Forwarding address found,$($_.ForwardingSmtpAddress) is set on $($_.UserPrincipalName) mailbox"
               }
       }

       if($removeSMTPFwdSettings.IsPresent){   
         
           if($_.ForwardingSmtpAddress -eq $null){
               write-host -fore magenta "No SMTP forwarding address set on account $($_.UserPrincipalName).  Please check and try again"
           }
           else{     
               Write-Host -fore yellow "SMTP Forwarding address found, $($_.ForwardingSmtpAddress) is set on $($_.UserPrincipalName) mailbox"
           
                   Try{
                   Write-Host -fore green "Removing SMTP forwarding address $($_.ForwardingSmtpAddress) from $($_.UserPrincipalName) mailbox"
                   Set-Mailbox -Identity $_.UserPrincipalName -ForwardingSmtpAddress $null
                   }
                   Catch{
                   write-host -fore Red "Failed to remove SMTP forwarding address on account $($_.UserPrincipalName)"
                   }
           }
       }
                   
       if($CheckForwardingAddress.IsPresent){
      
           if($_.ForwardingAddress -eq $null){
               write-host -fore green "No forwarding address set on account $($_.UserPrincipalName)"
               }
               else{
                   Write-Host -fore Yellow "Forwarding address found, $($_.ForwardingAddress) is set on $($_.UserPrincipalName) mailbox"
               }
       }
       
           if($RemoveForwardingAddress.IsPresent){
           
               if($_.ForwardingAddress -eq $null){
                   write-host -fore magenta "No forwarding address set on account $($_.UserPrincipalName).  Please check and try again"
                   }
                   else{     
                       Write-Host -fore yellow "Forwarding address found, $($_.ForwardingSmtpAddress) is set on $($_.UserPrincipalName) mailbox"
           
                           Try{
                           Write-Host -fore green "Removing forwarding address $($_.ForwardingAddress) from $($_.UserPrincipalName) mailbox"
                           Set-Mailbox -Identity $_.UserPrincipalName -ForwardingAddress $null
                           }
                           Catch{
                           write-host -fore Red "Failed to remove forwarding address on account $($_.UserPrincipalName)"
                           }
                   }
               }
   
           if($CheckDeliverToMailboxAndForward.IsPresent){

               if($_.DeliverToMailboxAndForward -eq $false){
                   write-host -fore green "Deliver To Mailbox And Forward setting is not configured on account $($_.UserPrincipalName)"
                   }
                   else{
                       Write-Host -fore Yellow "Deliver To Mailbox And Forward setting is configured on account $($_.UserPrincipalName)"
               }
           }
           
           if($DisableDeliverToMailboxAndForward.IsPresent){    
       
               if($_.DeliverToMailboxAndForward -eq $fasle){    
                   write-host -fore green "Deliver To Mailbox And Forward setting is not configured on account $($_.UserPrincipalName)"
               }
               else{
                   Write-Host -fore yellow "Deliver To Mailbox and Forward is set on account $($_.UserPrincipalName)"

                       Try{
                       Write-Host -for green "Setting Deliver To Mailbox and Forward on account $($_.UserPrincipalName) to false"
                       Set-Mailbox -Identity $_.UserPrincipalName -DeliverToMailboxAndForward $false
                       }
                       Catch{
                       Write-Host -fore Red "Fialed to set Deliver To Mailbox and Forward on account $($_.UserPrincipalName) to false"
                       }
               }
           }
           
       if($CheckInBoxRules.IsPresent){

           $forwardingRules = $null
           $ibxRules = $null

           Write-host -fore green "Checking $($_.UserPrincipalName) mailbox for inbox rules"
           $ibxRules = Get-InboxRule -Mailbox $_.UserPrincipalName
       
           if($ibxRules -ne $null){
               Write-Host -fore green "Found inboxrule(s) on Mailbox $($_.UserPrincipalName)"
               }
               else{
                    Write-Host -fore yellow "No inboxrule(s) found on Mailbox $($_.UserPrincipalName)"
               }
           If($ExportInBoxRules.IsPresent){
           
               if($ibxRules -ne $null){
                   foreach($ibxrule in $ibxRules){
                       $ibxRulesList = $null
                               
                           $ibxRulesList = [ordered]@{
                               PrimarySmtpAddress       = "$($_.UserPrincipalName)"
                               DisplayName              = "$($_.DisplayName)"
                               RuleName                 = "$($ibxrule.Name)"
                               RuleDescription          = "$($ibxrule.Description)"
                               RuleEnabled              = "$($ibxrule.Enabled)"
                               RuleForwardTo            = "$($ibxrule.forwardTo)"
                               RuleForwardAsAttchmentto = "$($ibxrule.forwardAsAttachmentTo)"
                               }
               
                               $ibxRulesList = New-Object PSObject -Property $ibxRulesList
                               $ibxRulesList | Export-Csv -Path $OutFileLocation -Append -NoTypeInformation           
                   }
                          
               }
           }

           if($RemoveInBoxRules.IsPresent){
               try{
                   foreach($ibxrule in $ibxRules){
                       Write-Host -ForegroundColor Cyan "Trying to remove $($ibxrule.name) from mailbox $($_.UserPrincipalName)"
                       Remove-InboxRule -Mailbox $_.UserPrincipalName -Identity $($ibxrule.name) -Force -Confirm:$false
                   }
               }
               catch{
               Write-Host -fore Magenta "Failed to remove inbox rules from $($_.UserPrincipalName)"
               }    

           }

       }

   }
}
Set-Alias -Name ClrMbx -Value Clean-MbxRules

Edited by HPlum78

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...