Skip to content

Commit 358966a

Browse files
committed
Refactor BEC remediation process to use Set-CIPPMailboxRule for disabling inbox rules, improving error handling and logging for user actions.
1 parent a06b461 commit 358966a

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Identity/Administration/Users/Invoke-ExecBECRemediate.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ function Invoke-ExecBECRemediate {
3535
if (($Rules | Measure-Object).Count -gt 0) {
3636
$Rules | Where-Object { $_.Name -ne 'Junk E-Mail Rule' -and $_.Name -notlike 'Microsoft.Exchange.OOF.*' } | ForEach-Object {
3737
try {
38-
$null = New-ExoRequest -anchor $Username -tenantid $TenantFilter -cmdlet 'Disable-InboxRule' -cmdParams @{Confirm = $false; Identity = $_.Identity }
39-
"Disabled Inbox Rule '$($_.Identity)' for $Username"
38+
Set-CIPPMailboxRule -Username $Username -TenantFilter $TenantFilter -RuleId $_.Identity -RuleName $_.Name -Disable -APIName $APIName -Headers $Headers
4039
$RuleDisabled++
4140
} catch {
42-
"Failed to disable Inbox Rule '$($_.Identity)' for $Username"
41+
$_.Exception.Message
4342
$RuleFailed++
4443
}
4544
}

Modules/CIPPCore/Public/Webhooks/Invoke-CIPPWebhookProcessing.ps1

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,31 @@ function Invoke-CippWebhookProcessing {
2929
Set-CIPPSignInState -TenantFilter $TenantFilter -User $data.UserId -AccountEnabled $false -APIName 'Alert Engine' -Headers 'Alert Engine'
3030
}
3131
'becremediate' {
32-
$username = (New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users/$($data.UserId)" -tenantid $TenantFilter).UserPrincipalName
33-
Set-CIPPResetPassword -UserID $username -tenantFilter $TenantFilter -APIName 'Alert Engine' -Headers 'Alert Engine'
34-
Set-CIPPSignInState -userid $username -AccountEnabled $false -tenantFilter $TenantFilter -APIName 'Alert Engine' -Headers 'Alert Engine'
35-
Revoke-CIPPSessions -userid $username -username $username -Headers 'Alert Engine' -APIName 'Alert Engine' -tenantFilter $TenantFilter
32+
$Username = (New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users/$($data.UserId)" -tenantid $TenantFilter).UserPrincipalName
33+
Set-CIPPResetPassword -UserID $Username -tenantFilter $TenantFilter -APIName 'Alert Engine' -Headers 'Alert Engine'
34+
Set-CIPPSignInState -userid $Username -AccountEnabled $false -tenantFilter $TenantFilter -APIName 'Alert Engine' -Headers 'Alert Engine'
35+
Revoke-CIPPSessions -userid $Username -username $Username -Headers 'Alert Engine' -APIName 'Alert Engine' -tenantFilter $TenantFilter
3636
$RuleDisabled = 0
37-
New-ExoRequest -anchor $username -tenantid $TenantFilter -cmdlet 'Get-InboxRule' -cmdParams @{Mailbox = $username; IncludeHidden = $true } | Where-Object { $_.Name -ne 'Junk E-Mail Rule' -and $_.Name -notlike 'Microsoft.Exchange.OOF.*' } | ForEach-Object {
38-
$null = New-ExoRequest -anchor $username -tenantid $TenantFilter -cmdlet 'Disable-InboxRule' -cmdParams @{Confirm = $false; Identity = $_.Identity }
39-
"Disabled Inbox Rule $($_.Identity) for $username"
40-
$RuleDisabled++
37+
$RuleFailed = 0
38+
New-ExoRequest -anchor $Username -tenantid $TenantFilter -cmdlet 'Get-InboxRule' -cmdParams @{Mailbox = $Username; IncludeHidden = $true } | Where-Object { $_.Name -ne 'Junk E-Mail Rule' -and $_.Name -notlike 'Microsoft.Exchange.OOF.*' } | ForEach-Object {
39+
try {
40+
Set-CIPPMailboxRule -Username $Username -TenantFilter $TenantFilter -RuleId $_.Identity -RuleName $_.Name -Disable -APIName 'Alert Engine' -Headers 'Alert Engine'
41+
$RuleDisabled++
42+
} catch {
43+
$_.Exception.Message
44+
$RuleFailed++
45+
}
4146
}
42-
if ($RuleDisabled) {
43-
"Disabled $RuleDisabled Inbox Rules for $username"
47+
if ($RuleDisabled -gt 0) {
48+
"Disabled $RuleDisabled Inbox Rules for $Username"
4449
} else {
45-
"No Inbox Rules found for $username. We have not disabled any rules."
50+
"No Inbox Rules found for $Username. We have not disabled any rules."
4651
}
47-
"Completed BEC Remediate for $username"
48-
Write-LogMessage -API 'BECRemediate' -tenant $tenantfilter -message "Executed Remediation for $username" -sev 'Info'
52+
if ($RuleFailed -gt 0) {
53+
"Failed to disable $RuleFailed Inbox Rules for $Username"
54+
}
55+
"Completed BEC Remediate for $Username"
56+
Write-LogMessage -API 'BECRemediate' -tenant $TenantFilter -message "Executed Remediation for $Username" -sev 'Info'
4957
}
5058
'cippcommand' {
5159
$CommandSplat = @{}

0 commit comments

Comments
 (0)