Create a new rule in Exchange Online Mailbox using EWS Managed API 2.2 and PowerShell
Create a new rule in Exchange Online Mailbox using EWS Managed API 2.2 and PowerShell
Summary
This TechNet Wiki article is to demo a PowerShell script which creates a new rule in Exchange Online Mailbox using EWS Managed API 2.2 and PowerShell. Using this script we can create the same rule for multiple mailboxes. A user complained about creating rules for all the mailboxes managed by him. At this time, he creates the same rule for more than one mailbox. So, shared a script which does the same in one go is the answer.
References
- Download EWS Managed API
- How to: Communicate with EWS using EWS Managed API
- Get started with EWS Managed API client applications
- EWS Managed API reference
Solution
To achieve this we need:
# Instantiate a Rule Class
$NewRule = [Microsoft.Exchange.WebServices.Data.Rule]::new()
# Instantiate a CreateRuleOperation Class
$CreateRuleOperation = [Microsoft.Exchange.WebServices.Data.CreateRuleOperation]::new($NewRule)
Here is the ugly code which uses the clear text password and no inline comments. But, it works as expected.
Import-Module 'C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll'$Service = [Microsoft.Exchange.WebServices.Data.ExchangeService]::new()$Service.Credentials = [System.Net.NetworkCredential]::new("chendrayan@contoso.onmicrosoft.com" , "SuperSecretPassword")$Service.Url = "https://outlook.office365.com/EWS/Exchange.asmx"$NewRule = [Microsoft.Exchange.WebServices.Data.Rule]::new()$NewRule.DisplayName = "Ad's"$NewRule.Priority = 1$NewRule.Exceptions.ContainsSubjectStrings$NewRule.Actions.MoveToFolder = [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::JunkEmail$CreateRuleOperation = [Microsoft.Exchange.WebServices.Data.CreateRuleOperation]::new($NewRule)$Service.UpdateInboxRules([Microsoft.Exchange.WebServices.Data.RuleOperation[]]@($CreateRuleOperation),$true)
And this set a rule with name "Ad's" and moves the email message item to the Junk folder.
Output
Full Code
function New-xInboxRule{ <# .SYNOPSIS A PowerShell function to create a rule in Exchange Online Mailbox .DESCRIPTION A PowerShell function to create a rule in an Exchange Online Mailbox. Allows to do the same on multiple Mailboxes. .EXAMPLE PS C:\> New-xInboxRule -Identity "chendrayan@contoso.onmicrosoft.com" -Priority 1 -Name "Demo" .EXAMPLE PS C:\> "karthik@contoso.onmicrosoft.com" , "chendrayan@contoso.onmicrosoft.com" | New-xInboxRule -Priority 1 -Name "Demo1" .EXAMPLE PS C:\> New-xInboxRule -Identity "chendrayan@contoso.onmicrosoft.com" -Priority 1 -Name "Demo" -Credential "admin@contoso.onmicrosoft.com" .NOTES @ChendrayanV http://chen.about-powershell.com #> [CmdletBinding()] param ( [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)] $Identity, [Parameter(Mandatory)] $Name, [Parameter()] $Priority, [Parameter()] [System.Management.Automation.CredentialAttribute()] [pscredential] $Credential ) begin { Import-Module 'C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll' } process { try { $Service = [Microsoft.Exchange.WebServices.Data.ExchangeService]::new() if($PSBoundParameters.ContainsKey('Credential')) { $Service.Credentials = $Credential } else { $Service.UseDefaultCredentials = $true } $Service.Url = "https://outlook.office365.com/EWS/Exchange.asmx" $Service.ImpersonatedUserId = [Microsoft.Exchange.WebServices.Data.ImpersonatedUserId]::new([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$Identity) $NewRule = [Microsoft.Exchange.WebServices.Data.Rule]::new() $NewRule.DisplayName = $Name $NewRule.Priority = $Priority $NewRule.Conditions.ContainsSubjectStrings.Add("Ad's") $NewRule.Actions.MoveToFolder = [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::JunkEmail $CreateRuleOperation = [Microsoft.Exchange.WebServices.Data.CreateRuleOperation]::new($NewRule) $Service.UpdateInboxRules([Microsoft.Exchange.WebServices.Data.RuleOperation[]]@($CreateRuleOperation),$true) } catch { $_.Exception.Message } } end { }}
Usage
help New-xInboxRule
help New-xInboxRule -Examples