Skip to content

Commit

Permalink
Merge pull request #1091 from kris6673/delete-mailboxrules
Browse files Browse the repository at this point in the history
Ability to delete mailboxrules
  • Loading branch information
KelvinTegelaar authored Aug 25, 2024
2 parents be56ca1 + 97997fc commit e24facf
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using namespace System.Net

Function Invoke-ExecRemoveMailboxRule {
<#
.FUNCTIONALITY
Entrypoint
.ROLE
Exchange.Mailbox.ReadWrite
#>
[CmdletBinding()]
param($Request, $TriggerMetadata)

$APIName = 'Remove mailbox rule'
$TenantFilter = $Request.Query.TenantFilter
$RuleName = $Request.Query.ruleName
$RuleId = $Request.Query.ruleId
$Username = $Request.Query.userPrincipalName

$User = $request.headers.'x-ms-client-principal'
Write-LogMessage -user $User -API $APINAME -tenant $TenantFilter -message 'Accessed this API' -Sev 'Debug'

# Write to the Azure Functions log stream.
Write-Host 'PowerShell HTTP trigger function processed a request.'

# Remove the rule
$Results = Remove-CIPPMailboxRule -userid $User -username $Username -TenantFilter $TenantFilter -APIName $APINAME -ExecutingUser $User -RuleId $RuleId -RuleName $RuleName

if ($Results -like '*Could not delete*') {
$StatusCode = [HttpStatusCode]::Forbidden
} else {
$StatusCode = [HttpStatusCode]::OK
}

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = $StatusCode
Body = @{ Results = $Results }
})

}
2 changes: 1 addition & 1 deletion Modules/CIPPCore/Public/Invoke-CIPPOffboardingJob.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function Invoke-CIPPOffboardingJob {

{ $_.'removeRules' -eq 'true' } {
Write-Host "Removing rules for $username"
Remove-CIPPRules -userid $userid -username $Username -tenantFilter $Tenantfilter -ExecutingUser $ExecutingUser -APIName $APIName
Remove-CIPPMailboxRule -userid $userid -username $Username -tenantFilter $Tenantfilter -ExecutingUser $ExecutingUser -APIName $APIName -RemoveAllRules
}

{ $_.'removeMobile' -eq 'true' } {
Expand Down
48 changes: 48 additions & 0 deletions Modules/CIPPCore/Public/Remove-CIPPMailboxRule.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function Remove-CIPPMailboxRule {
[CmdletBinding()]
param (
$userid,
$username,
$TenantFilter,
$APIName = 'Mailbox Rules Removal',
$ExecutingUser,
$RuleId,
$RuleName,
[switch]$RemoveAllRules
)

if ($RemoveAllRules.IsPresent -eq $true) {
# Delete all rules
try {
Write-Host "Checking rules for $username"
$rules = New-ExoRequest -tenantid $TenantFilter -cmdlet 'Get-InboxRule' -cmdParams @{Mailbox = $username; IncludeHidden = $true } | Where-Object { $_.Name -ne 'Junk E-Mail Rule' }
Write-Host "$($rules.count) rules found"
if ($null -eq $rules) {
Write-LogMessage -user $ExecutingUser -API $APIName -message "No Rules for $($username) to delete" -Sev 'Info' -tenant $TenantFilter
return "No rules for $($username) to delete"
} else {
ForEach ($rule in $rules) {
New-ExoRequest -tenantid $TenantFilter -cmdlet 'Remove-InboxRule' -Anchor $username -cmdParams @{Identity = $rule.Identity }
}
Write-LogMessage -user $ExecutingUser -API $APIName -message "Deleted Rules for $($username)" -Sev 'Info' -tenant $TenantFilter
return "Deleted Rules for $($username)"
}
} catch {
$ErrorMessage = Get-CippException -Exception $_
Write-LogMessage -user $ExecutingUser -API $APIName -message "Could not delete rules for $($username): $($ErrorMessage.NormalizedError)" -Sev 'Error' -tenant $TenantFilter -LogData $ErrorMessage
return "Could not delete rules for $($username). Error: $($ErrorMessage.NormalizedError)"
}
} else {
# Only delete 1 rule
try {
$null = New-ExoRequest -tenantid $TenantFilter -cmdlet 'Remove-InboxRule' -Anchor $username -cmdParams @{Identity = $RuleId }
Write-LogMessage -user $ExecutingUser -API $APIName -message "Deleted mailbox rule $($RuleName) for $($username)" -Sev 'Info' -tenant $TenantFilter
return "Deleted mailbox rule $($RuleName) for $($username)"
} catch {
$ErrorMessage = Get-CippException -Exception $_
Write-LogMessage -user $ExecutingUser -API $APIName -message "Could not delete rule for $($username): $($ErrorMessage.NormalizedError)" -Sev 'Error' -tenant $TenantFilter -LogData $ErrorMessage
return "Could not delete rule for $($username). Error: $($ErrorMessage.NormalizedError)"
}
}
}

30 changes: 0 additions & 30 deletions Modules/CIPPCore/Public/Remove-CIPPRules.ps1

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ function Invoke-CIPPStandardNudgeMFA {
Return
}
# Input validation
[int]$snoozeDurationInDays = $Settings.snoozeDurationInDays
if (($snoozeDurationInDays -lt 0 -or $snoozeDurationInDays -gt 14) -and ($Settings.remediate -eq $true -or $Settings.alert -eq $true)) {
if (([Int32]$Settings.snoozeDurationInDays -lt 0 -or [Int32]$Settings.snoozeDurationInDays -gt 15) -and ($Settings.remediate -eq $true -or $Settings.alert -eq $true)) {
Write-LogMessage -API 'Standards' -tenant $tenant -message 'NudgeMFA: Invalid snoozeDurationInDays parameter set' -sev Error
Return
}
Expand Down

0 comments on commit e24facf

Please sign in to comment.