Skip to content

Commit 6192717

Browse files
committed
Fixes for spam filter policy when using custom names
1 parent 3204da3 commit 6192717

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

Modules/CIPPStandards/Public/Standards/Invoke-CIPPStandardSpamFilterPolicy.ps1

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,35 @@ function Invoke-CIPPStandardSpamFilterPolicy {
8282
} #we're done.
8383

8484
# Use custom name if provided, otherwise use default for backward compatibility
85-
$PolicyName = if ($Settings.name) { $Settings.name } else { 'CIPP Default Spam Filter Policy' }
85+
$DefaultPolicyName = 'CIPP Default Spam Filter Policy'
86+
$PolicyName = if ($Settings.name) { $Settings.name } else { $DefaultPolicyName }
8687

8788
try {
88-
$CurrentState = New-ExoRequest -TenantId $Tenant -cmdlet 'Get-HostedContentFilterPolicy' |
89-
Where-Object -Property Name -EQ $PolicyName
89+
$AllSpamFilterPolicies = New-ExoRequest -TenantId $Tenant -cmdlet 'Get-HostedContentFilterPolicy'
9090
} catch {
9191
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
9292
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the SpamFilterPolicy state for $Tenant. Error: $ErrorMessage" -Sev Error
9393
return
9494
}
9595

96+
# Only match against legacy/default names when no custom name is provided. When a custom name is
97+
# set, deploy it as a new policy instead of reusing an existing default-named one. 'Default' is
98+
# Microsoft's built-in inbound anti-spam policy ("Anti-spam inbound policy" in the portal); it
99+
# cannot be renamed and has no associated rule.
100+
if ($PolicyName -eq $DefaultPolicyName) {
101+
$PolicyList = @($PolicyName, 'Default Spam Filter Policy', 'Default')
102+
$ExistingPolicy = $AllSpamFilterPolicies | Where-Object -Property Name -In $PolicyList | Select-Object -First 1
103+
if ($null -ne $ExistingPolicy.Name) {
104+
# Use existing policy name if found
105+
$PolicyName = $ExistingPolicy.Name
106+
}
107+
}
108+
109+
# The built-in default policy cannot have a HostedContentFilterRule, so rule remediation is skipped for it.
110+
$IsDefaultPolicy = $PolicyName -eq 'Default'
111+
112+
$CurrentState = $AllSpamFilterPolicies | Where-Object -Property Name -EQ $PolicyName
113+
96114
$SpamAction = $Settings.SpamAction.value ?? $Settings.SpamAction
97115
$SpamQuarantineTag = $Settings.SpamQuarantineTag.value ?? $Settings.SpamQuarantineTag
98116
$HighConfidenceSpamAction = $Settings.HighConfidenceSpamAction.value ?? $Settings.HighConfidenceSpamAction
@@ -253,7 +271,7 @@ function Invoke-CIPPStandardSpamFilterPolicy {
253271
}
254272
}
255273

256-
if ($RuleStateIsCorrect -eq $false) {
274+
if ($RuleStateIsCorrect -eq $false -and -not $IsDefaultPolicy) {
257275
$cmdParams = @{
258276
Priority = 0
259277
RecipientDomainIs = ConvertTo-SafeArray -Field $AcceptedDomains.Name

0 commit comments

Comments
 (0)