-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1091 from kris6673/delete-mailboxrules
Ability to delete mailboxrules
- Loading branch information
Showing
5 changed files
with
90 additions
and
33 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...IPPCore/Public/Entrypoints/HTTP Functions/Email-Exchange/Invoke-ExecRemoveMailboxRule.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | ||
} | ||
} | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters