diff --git a/Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Push-ListMailboxRulesQueue.ps1 b/Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Push-ListMailboxRulesQueue.ps1 index c64592b7aab2..1c826fbfa0cb 100644 --- a/Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Push-ListMailboxRulesQueue.ps1 +++ b/Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Push-ListMailboxRulesQueue.ps1 @@ -26,7 +26,7 @@ function Push-ListMailboxRulesQueue { Rules = [string]($Rule | ConvertTo-Json) RowKey = [string](New-Guid).guid Tenant = [string]$domainName - PartitionKey = 'mailboxrules' + PartitionKey = 'MailboxRules' } } @@ -38,7 +38,7 @@ function Push-ListMailboxRulesQueue { Rules = [string]$Rules RowKey = [string]$domainName Tenant = [string]$domainName - PartitionKey = 'mailboxrules' + PartitionKey = 'MailboxRules' } } } catch { @@ -49,7 +49,7 @@ function Push-ListMailboxRulesQueue { Rules = [string]$Rules RowKey = [string]$domainName Tenant = [string]$domainName - PartitionKey = 'mailboxrules' + PartitionKey = 'MailboxRules' } } Add-CIPPAzDataTableEntity @Table -Entity $GraphRequest -Force | Out-Null diff --git a/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Email-Exchange/Administration/Invoke-ListMailboxRules.ps1 b/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Email-Exchange/Administration/Invoke-ListMailboxRules.ps1 index c92734c3b651..0c0b1c954322 100644 --- a/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Email-Exchange/Administration/Invoke-ListMailboxRules.ps1 +++ b/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Email-Exchange/Administration/Invoke-ListMailboxRules.ps1 @@ -22,13 +22,24 @@ Function Invoke-ListMailboxRules { $Table.Filter = "Tenant eq '$TenantFilter'" } $Rows = Get-CIPPAzDataTableEntity @Table | Where-Object -Property Timestamp -GT (Get-Date).AddHours(-1) + $PartitionKey = 'MailboxRules' + $QueueReference = '{0}-{1}' -f $TenantFilter, $PartitionKey + $RunningQueue = Invoke-ListCippQueue | Where-Object { $_.Reference -eq $QueueReference -and $_.Status -notmatch 'Completed' -and $_.Status -notmatch 'Failed' } $Metadata = @{} - if (!$Rows -or ($TenantFilter -eq 'AllTenants' -and ($Rows | Measure-Object).Count -eq 1)) { + # If a queue is running, we will not start a new one + if ($RunningQueue) { $Metadata = [PSCustomObject]@{ - QueueMessage = 'Loading data. Please check back in 1 minute' + QueueMessage = "Still loading data for $TenantFilter. Please check back in a few more minutes" + } + [PSCustomObject]@{ + Waiting = $true + } + } elseif ((!$Rows -and !$RunningQueue) -or ($TenantFilter -eq 'AllTenants' -and ($Rows | Measure-Object).Count -eq 1)) { + # If no rows are found and no queue is running, we will start a new one + $Metadata = [PSCustomObject]@{ + QueueMessage = "Loading data for $TenantFilter. Please check back in 1 minute" } - $GraphRequest = @() if ($TenantFilter -eq 'AllTenants') { $Tenants = Get-Tenants -IncludeErrors | Select-Object defaultDomainName @@ -37,7 +48,7 @@ Function Invoke-ListMailboxRules { $Tenants = @(@{ defaultDomainName = $TenantFilter }) $Type = $TenantFilter } - $Queue = New-CippQueueEntry -Name "Mailbox Rules ($Type)" -TotalTasks ($Tenants | Measure-Object).Count + $Queue = New-CippQueueEntry -Name "Mailbox Rules ($Type)" -Reference $QueueReference -TotalTasks ($Tenants | Measure-Object).Count $Batch = $Tenants | Select-Object defaultDomainName, @{Name = 'FunctionName'; Expression = { 'ListMailboxRulesQueue' } }, @{Name = 'QueueName'; Expression = { $_.defaultDomainName } }, @{Name = 'QueueId'; Expression = { $Queue.RowKey } } if (($Batch | Measure-Object).Count -gt 0) { $InputObject = [PSCustomObject]@{ @@ -53,6 +64,7 @@ Function Invoke-ListMailboxRules { } else { if ($TenantFilter -ne 'AllTenants') { $Rows = $Rows | Where-Object -Property Tenant -EQ $TenantFilter + $Rows = $Rows } $GraphRequest = $Rows | ForEach-Object { $NewObj = $_.Rules | ConvertFrom-Json -ErrorAction SilentlyContinue @@ -61,6 +73,8 @@ Function Invoke-ListMailboxRules { } } + # If no results are found, we will return an empty message to prevent null reference errors in the frontend + $GraphRequest = $GraphRequest ?? @() $Body = @{ Results = @($GraphRequest) Metadata = $Metadata