-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cleanup-OWARules.ps1
29 lines (23 loc) · 1.12 KB
/
Cleanup-OWARules.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Connect to Exchange Online
$UserCredential = Get-Credential
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $ExchangeSession -DisableNameChecking
# Retrieve all user mailboxes
$Mailboxes = Get-Mailbox -ResultSize Unlimited
# Loop through each user mailbox
foreach ($Mailbox in $Mailboxes) {
Write-Host "Processing mailbox: $($Mailbox.PrimarySmtpAddress)"
# Get inbox rules
$InboxRules = Get-InboxRule -Mailbox $Mailbox.PrimarySmtpAddress
# Loop through each rule
foreach ($Rule in $InboxRules) {
# Check if the rule is in an error state
if ($Rule.RuleErrorActionRequired -eq $true) {
Write-Host "Removing erroring rule: $($Rule.Name)"
# Remove erroring rule
Remove-InboxRule -Mailbox $Mailbox.PrimarySmtpAddress -Identity $Rule.Identity -Confirm:$false
}
}
}
# End the Exchange Online session
Remove-PSSession $ExchangeSession