locked
EWS script to create inbox rule - is not forwarding RRS feed

  • Question

  • Hi there

    am writing an EWS script to create an inbox rule in recipients mailboxes - to either forward or redirect messages to an external mailbox using a contact record with an external smtp address - but it is not forwarding- i tested with my gmail account - see below portion of script:

        $inboxRule = New-Object Microsoft.Exchange.WebServices.Data.Rule
        $inboxRule.DisplayName = "Termination Auto Reply"
        $inboxRule.IsEnabled = $true
        $inboxRule.Conditions.SentToOrCcMe = $true
        $inboxRule.Actions.RedirectToRecipients.add("blahblah@gmail.com")
       # $inboxRule.Actions.PermanentDelete = $true
        $inboxRule.Exceptions.IsNonDeliveryReport = $true
        $createRule = New-Object Microsoft.Exchange.WebServices.Data.CreateRuleOperation[] 1
        $createRule[0] = $inboxRule
        $service.UpdateInboxRules($createRule,$true)

    It creates the rule correctly when checked in OWA but the mail is not being redirected or forwarded? Any ideas?

    Tuesday, July 17, 2018 3:27 PM

Answers

  • I've fixed the above issue
    • Marked as answer by nickkinn Friday, July 27, 2018 5:10 PM
    Thursday, July 26, 2018 5:54 PM

All replies

  • You need to check the remote domain settings https://docs.microsoft.com/en-us/exchange/mail-flow-best-practices/remote-domains/remote-domains (or Onprem read http://techgenix.com/remote-domains-exchange-2013-part1/ ). It sounds like you don't allow Automatic forwards to be sent to the domain in question.

    Cheers
    Glen

    Tuesday, July 17, 2018 11:31 PM
  • Thanks a lot the remote domain allows the forwarding however i ran into another issue

    the line:  $inboxRule.Conditions.SentToOrCcMe = $true

    only works as it says on the condition the recipients name is in the To or CC fields and does not take into account DL's that the user is a member of - so they were still receiving email - i realised i couldnt put in another condition of  $inboxRule.Conditions.NotSentToMe = $true in the same rule as they just don't work together

    so tried to put another rule creation into the same script like this

                                                   

    foreach($user in $users) #loop thru each user in CSV file
        {

            $i = Get-Mailbox $user.Name | select primarysmtpaddress, alias #extract primarysmtp and alias

            $alias = $i.alias

            $id = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId -ArgumentList "SmtpAddress",$i.primarysmtpaddress   #using Impersonation to create rule
            $Service.ImpersonatedUserId = $id
            $Service.Url = "https://mail.exdevchild.lmig.com/ews/exchange.asmx"
            #$Service.AutodiscoverUrl($i.primarysmtpaddress)              
            #Create Inbox Rule
            $inboxRule = New-Object Microsoft.Exchange.WebServices.Data.Rule
            $inboxRule.DisplayName = "Termination Auto Reply"
            $inboxRule.IsEnabled = $true
            $inboxRule.Priority = 1
             $inboxRule.Conditions.SentToOrCcMe = $true
             $inboxRule.Actions.RedirectToRecipients.add("blahblah@gmail.com") | Out-Null   #this can be replaced to "$alias.lfg.com" for each user in the list
             $inboxRule.Actions.PermanentDelete = $true
             $inboxRule.Exceptions.IsNonDeliveryReport = $true
            $inboxRule.Conditions.NotSentToMe




            $createRule = New-Object Microsoft.Exchange.WebServices.Data.CreateRuleOperation[] 1
            $createRule[0] = $inboxRule
            $service.UpdateInboxRules($createRule,$true)


                $inboxRule1 = New-Object Microsoft.Exchange.WebServices.Data.Rule
            $inboxRule1.DisplayName = "DL Redirect"
            $inboxRule1.IsEnabled = $true
            $inboxRule1.Priority = 2
             $inboxRule1.Conditions.NotSentToMe = $true
             $inboxRule1.Actions.RedirectToRecipients.add("blahblah@gmail.com") | Out-Null   #this can be replaced to "$alias.lfg.com" for each user in the list
             $inboxRule1.Actions.PermanentDelete = $true

             $createRule = New-Object Microsoft.Exchange.WebServices.Data.CreateRuleOperation[] 2
            $createRule[1] = $inboxRule1
            $service.UpdateInboxRules($createRule,$true)



            $i = $null
     }

    however now get 

    Exception calling "UpdateInboxRules" with "2" argument(s): "Value cannot be 
    null.
    Parameter name: RuleOperation"

    it was working earlier - cant remember exactly what i changed but found that even though i specified rule priority 1 and 2 they were being put way down priority of list of rules the users had - even though i want these 2 at the top of all the users list to process first - basically redirect everything except NDR's and delete the message? Any help would be greatly appreciated - also i didnt need the mail contact after all as the remote domain setting allows you to redirect the message externally anyway

    Wednesday, July 25, 2018 11:32 PM
  • I've fixed the above issue
    • Marked as answer by nickkinn Friday, July 27, 2018 5:10 PM
    Thursday, July 26, 2018 5:54 PM