Skip to content

Commit 947c269

Browse files
committed
Updated to use Bulk requests for more efficient handling.
Updated to take Mailbox Plans into account to ensure we are not attempting to set values higher than mailbox plans are already set at.
1 parent 5c6e401 commit 947c269

File tree

1 file changed

+100
-14
lines changed

1 file changed

+100
-14
lines changed

Modules/CIPPCore/Public/Standards/Invoke-CIPPStandardMailboxRecipientLimits.ps1

Lines changed: 100 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,100 @@ function Invoke-CIPPStandardMailboxRecipientLimits {
3333
return
3434
}
3535

36-
# Get all mailboxes in the tenant
37-
$Mailboxes = New-ExoRequest -tenantid $Tenant -cmdlet 'Get-Mailbox' -cmdParams @{ResultSize = 'Unlimited' }
36+
# Get all mailboxes and their associated mailbox plans in a single batch
37+
$Requests = @(
38+
@{
39+
CmdletInput = @{
40+
CmdletName = 'Get-Mailbox'
41+
Parameters = @{ ResultSize = 'Unlimited' }
42+
}
43+
},
44+
@{
45+
CmdletInput = @{
46+
CmdletName = 'Get-MailboxPlan'
47+
Parameters = @{ ResultSize = 'Unlimited' }
48+
}
49+
}
50+
)
3851

39-
# Check which mailboxes need to be updated
40-
$MailboxesToUpdate = $Mailboxes | Where-Object { $_.RecipientLimits -ne $Settings.RecipientLimit }
52+
$Results = New-ExoBulkRequest -tenantid $Tenant -cmdletArray $Requests
53+
$Mailboxes = $Results.GetMailbox
54+
$MailboxPlans = $Results.GetMailboxPlan
55+
56+
# Create a hashtable of mailbox plans for quick lookup
57+
$MailboxPlanLookup = @{}
58+
foreach ($Plan in $MailboxPlans) {
59+
$MailboxPlanLookup[$Plan.Guid] = $Plan
60+
}
61+
62+
# Process mailboxes and categorize them based on their plan limits
63+
$MailboxResults = $Mailboxes | ForEach-Object {
64+
$Mailbox = $_
65+
$Plan = $MailboxPlanLookup[$Mailbox.MailboxPlanId]
66+
67+
if ($Plan) {
68+
$PlanMaxRecipients = $Plan.MaxRecipientsPerMessage
69+
70+
if ($Settings.RecipientLimit -gt $PlanMaxRecipients) {
71+
[PSCustomObject]@{
72+
Type = 'PlanIssue'
73+
Mailbox = $Mailbox
74+
PlanLimit = $PlanMaxRecipients
75+
PlanName = $Plan.DisplayName
76+
}
77+
}
78+
elseif ($Mailbox.RecipientLimits -ne $Settings.RecipientLimit) {
79+
[PSCustomObject]@{
80+
Type = 'ToUpdate'
81+
Mailbox = $Mailbox
82+
}
83+
}
84+
}
85+
elseif ($Mailbox.RecipientLimits -ne $Settings.RecipientLimit) {
86+
[PSCustomObject]@{
87+
Type = 'ToUpdate'
88+
Mailbox = $Mailbox
89+
}
90+
}
91+
}
92+
93+
# Separate mailboxes into their respective categories
94+
$MailboxesToUpdate = $MailboxResults | Where-Object { $_.Type -eq 'ToUpdate' } | Select-Object -ExpandProperty Mailbox
95+
$MailboxesWithPlanIssues = $MailboxResults | Where-Object { $_.Type -eq 'PlanIssue' } | ForEach-Object {
96+
[PSCustomObject]@{
97+
Identity = $_.Mailbox.Identity
98+
CurrentLimit = $_.Mailbox.RecipientLimits
99+
PlanLimit = $_.PlanLimit
100+
PlanName = $_.PlanName
101+
}
102+
}
41103

42104
# Remediation
43105
if ($Settings.remediate -eq $true) {
106+
if ($MailboxesWithPlanIssues.Count -gt 0) {
107+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Found $($MailboxesWithPlanIssues.Count) mailboxes where the requested recipient limit ($($Settings.RecipientLimit)) exceeds their mailbox plan limit. These mailboxes will not be updated." -sev Info
108+
foreach ($Mailbox in $MailboxesWithPlanIssues) {
109+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Mailbox $($Mailbox.Identity) has plan $($Mailbox.PlanName) with maximum limit of $($Mailbox.PlanLimit)" -sev Info
110+
}
111+
}
112+
44113
if ($MailboxesToUpdate.Count -gt 0) {
45114
try {
46-
foreach ($Mailbox in $MailboxesToUpdate) {
47-
$null = New-ExoRequest -tenantid $Tenant -cmdlet 'Set-Mailbox' -cmdParams @{
48-
Identity = $Mailbox.Identity
49-
RecipientLimits = $Settings.RecipientLimit
115+
# Create batch requests for mailbox updates
116+
$UpdateRequests = $MailboxesToUpdate | ForEach-Object {
117+
@{
118+
CmdletInput = @{
119+
CmdletName = 'Set-Mailbox'
120+
Parameters = @{
121+
Identity = $_.Identity
122+
RecipientLimits = $Settings.RecipientLimit
123+
}
124+
}
50125
}
51126
}
127+
128+
# Execute batch update
129+
$null = New-ExoBulkRequest -tenantid $Tenant -cmdletArray $UpdateRequests
52130
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Successfully set recipient limits to $($Settings.RecipientLimit) for $($MailboxesToUpdate.Count) mailboxes" -sev Info
53131
}
54132
catch {
@@ -63,24 +141,32 @@ function Invoke-CIPPStandardMailboxRecipientLimits {
63141

64142
# Alert
65143
if ($Settings.alert -eq $true) {
66-
if ($MailboxesToUpdate.Count -eq 0) {
144+
if ($MailboxesToUpdate.Count -eq 0 -and $MailboxesWithPlanIssues.Count -eq 0) {
67145
Write-LogMessage -API 'Standards' -tenant $Tenant -message "All mailboxes have the correct recipient limit of $($Settings.RecipientLimit)" -sev Info
68146
}
69147
else {
70-
Write-StandardsAlert -message "Found $($MailboxesToUpdate.Count) mailboxes with incorrect recipient limits" -object $MailboxesToUpdate -tenant $Tenant -standardName 'MailboxRecipientLimits' -standardId $Settings.standardId
71-
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Found $($MailboxesToUpdate.Count) mailboxes with incorrect recipient limits" -sev Info
148+
$AlertMessage = "Found $($MailboxesToUpdate.Count) mailboxes with incorrect recipient limits"
149+
if ($MailboxesWithPlanIssues.Count -gt 0) {
150+
$AlertMessage += " and $($MailboxesWithPlanIssues.Count) mailboxes where the requested limit exceeds their mailbox plan limit"
151+
}
152+
Write-StandardsAlert -message $AlertMessage -object ($MailboxesToUpdate + $MailboxesWithPlanIssues) -tenant $Tenant -standardName 'MailboxRecipientLimits' -standardId $Settings.standardId
153+
Write-LogMessage -API 'Standards' -tenant $Tenant -message $AlertMessage -sev Info
72154
}
73155
}
74156

75157
# Report
76158
if ($Settings.report -eq $true) {
77-
Add-CIPPBPAField -FieldName 'MailboxRecipientLimits' -FieldValue $MailboxesToUpdate -StoreAs json -Tenant $Tenant
159+
$ReportData = @{
160+
MailboxesToUpdate = $MailboxesToUpdate
161+
MailboxesWithPlanIssues = $MailboxesWithPlanIssues
162+
}
163+
Add-CIPPBPAField -FieldName 'MailboxRecipientLimits' -FieldValue $ReportData -StoreAs json -Tenant $Tenant
78164

79-
if ($MailboxesToUpdate.Count -eq 0) {
165+
if ($MailboxesToUpdate.Count -eq 0 -and $MailboxesWithPlanIssues.Count -eq 0) {
80166
$FieldValue = $true
81167
}
82168
else {
83-
$FieldValue = $MailboxesToUpdate
169+
$FieldValue = $ReportData
84170
}
85171
Set-CIPPStandardsCompareField -FieldName 'standards.MailboxRecipientLimits' -FieldValue $FieldValue -Tenant $Tenant
86172
}

0 commit comments

Comments
 (0)