Skip to content

Commit a424316

Browse files
excluded tenants groups to scheduler and alerts
Synced from CyberDrain/CIPP@f5833a7
1 parent 8e17d79 commit a424316

8 files changed

Lines changed: 56 additions & 11 deletions

File tree

Modules/CIPPCore/Public/Add-CIPPScheduledTask.ps1

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,15 @@ function Add-CIPPScheduledTask {
188188
$task.ScheduledTime = [int64](([datetime]::UtcNow) - (Get-Date '1/1/1970')).TotalSeconds
189189
}
190190
}
191-
$excludedTenants = if ($task.excludedTenants.value) {
192-
$task.excludedTenants.value -join ','
191+
# Split exclusions by type (same pattern as Tenant/TenantGroup): plain tenants are
192+
# comma-joined, groups are stored as JSON and expanded at runtime by the orchestrator
193+
$ExcludedEntries = @($task.excludedTenants | Where-Object { $_.value })
194+
$excludedTenants = @($ExcludedEntries | Where-Object { $_.type -ne 'Group' }).value -join ','
195+
$ExcludedGroupEntries = @($ExcludedEntries | Where-Object { $_.type -eq 'Group' } | ForEach-Object {
196+
[PSCustomObject]@{ value = $_.value; label = $_.label; type = 'Group' }
197+
})
198+
$excludedTenantGroups = if ($ExcludedGroupEntries.Count -gt 0) {
199+
ConvertTo-Json -InputObject $ExcludedGroupEntries -Compress -Depth 5
193200
}
194201

195202
# Handle tenant filter - support both single tenant and tenant groups
@@ -222,6 +229,7 @@ function Add-CIPPScheduledTask {
222229
RowKey = [string]$RowKey
223230
Tenant = [string]$tenantFilter
224231
excludedTenants = [string]$excludedTenants
232+
excludedTenantGroups = [string]$excludedTenantGroups
225233
Name = [string]$task.Name
226234
Command = [string]$RequestedCommand
227235
Parameters = [string]$Parameters

Modules/CIPPCore/Public/Entrypoints/Orchestrator Functions/Start-AuditLogIngestion.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ function Start-AuditLogIngestion {
2929
if (!$ConfigEntry.excludedTenants) {
3030
$ConfigEntry | Add-Member -MemberType NoteProperty -Name 'excludedTenants' -Value @() -Force
3131
} else {
32-
$ConfigEntry.excludedTenants = $ConfigEntry.excludedTenants | ConvertFrom-Json
32+
# Expand tenant groups in exclusions so group members match on defaultDomainName
33+
$Excluded = $ConfigEntry.excludedTenants | ConvertFrom-Json -ErrorAction SilentlyContinue
34+
$ConfigEntry.excludedTenants = if ($Excluded) { @(Expand-CIPPTenantGroups -TenantFilter $Excluded) } else { @() }
3335
}
3436
$ConfigEntry.Tenants = $ConfigEntry.Tenants | ConvertFrom-Json
3537
$ConfigEntry

Modules/CIPPCore/Public/Entrypoints/Orchestrator Functions/Start-AuditLogSearchCreation.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ function Start-AuditLogSearchCreation {
1515
if (!$ConfigEntry.excludedTenants) {
1616
$ConfigEntry | Add-Member -MemberType NoteProperty -Name 'excludedTenants' -Value @() -Force
1717
} else {
18-
$ConfigEntry.excludedTenants = $ConfigEntry.excludedTenants | ConvertFrom-Json
18+
# Expand tenant groups in exclusions so group members match on defaultDomainName
19+
$Excluded = $ConfigEntry.excludedTenants | ConvertFrom-Json -ErrorAction SilentlyContinue
20+
$ConfigEntry.excludedTenants = if ($Excluded) { @(Expand-CIPPTenantGroups -TenantFilter $Excluded) } else { @() }
1921
}
2022
$ConfigEntry.Tenants = $ConfigEntry.Tenants | ConvertFrom-Json
2123
$ConfigEntry

Modules/CIPPCore/Public/Entrypoints/Orchestrator Functions/Start-AuditLogSearchCreationV2.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ function Start-AuditLogSearchCreationV2 {
2525
if (!$ConfigEntry.excludedTenants) {
2626
$ConfigEntry | Add-Member -MemberType NoteProperty -Name 'excludedTenants' -Value @() -Force
2727
} else {
28-
$ConfigEntry.excludedTenants = $ConfigEntry.excludedTenants | ConvertFrom-Json
28+
# Expand tenant groups in exclusions so group members match on defaultDomainName
29+
$Excluded = $ConfigEntry.excludedTenants | ConvertFrom-Json -ErrorAction SilentlyContinue
30+
$ConfigEntry.excludedTenants = if ($Excluded) { @(Expand-CIPPTenantGroups -TenantFilter $Excluded) } else { @() }
2931
}
3032
$ConfigEntry.Tenants = $ConfigEntry.Tenants | ConvertFrom-Json
3133
$ConfigEntry | Add-Member -MemberType NoteProperty -Name 'ExpandedTenants' -Value (Expand-CIPPTenantGroups -TenantFilter ($ConfigEntry.Tenants)).value -Force

Modules/CIPPCore/Public/Entrypoints/Orchestrator Functions/Start-UserTasksOrchestrator.ps1

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,14 @@ function Start-UserTasksOrchestrator {
115115
}
116116

117117
if ($task.Tenant -eq 'AllTenants') {
118-
$ExcludedTenants = $task.excludedTenants -split ','
118+
$ExcludedTenants = @($task.excludedTenants -split ',' | Where-Object { $_ })
119+
if ($task.excludedTenantGroups) {
120+
# Expand excluded tenant groups at runtime so membership changes are honored
121+
$ExcludedGroups = $task.excludedTenantGroups | ConvertFrom-Json -ErrorAction SilentlyContinue
122+
if ($ExcludedGroups) {
123+
$ExcludedTenants = @($ExcludedTenants + (Expand-CIPPTenantGroups -TenantFilter $ExcludedGroups).value | Where-Object { $_ })
124+
}
125+
}
119126
Write-Host "Excluded Tenants from this task: $ExcludedTenants"
120127
$AllTenantCommands = foreach ($Tenant in $TenantList | Where-Object { $_.defaultDomainName -notin $ExcludedTenants }) {
121128
$NewParams = $task.Parameters.Clone()
@@ -148,7 +155,14 @@ function Start-UserTasksOrchestrator {
148155
# Expand the tenant group to individual tenants
149156
$ExpandedTenants = Expand-CIPPTenantGroups -TenantFilter $TenantFilterForExpansion
150157

151-
$ExcludedTenants = $task.excludedTenants -split ','
158+
$ExcludedTenants = @($task.excludedTenants -split ',' | Where-Object { $_ })
159+
if ($task.excludedTenantGroups) {
160+
# Expand excluded tenant groups at runtime so membership changes are honored
161+
$ExcludedGroups = $task.excludedTenantGroups | ConvertFrom-Json -ErrorAction SilentlyContinue
162+
if ($ExcludedGroups) {
163+
$ExcludedTenants = @($ExcludedTenants + (Expand-CIPPTenantGroups -TenantFilter $ExcludedGroups).value | Where-Object { $_ })
164+
}
165+
}
152166
Write-Host "Excluded Tenants from this task: $ExcludedTenants"
153167

154168
$GroupTenantCommands = foreach ($ExpandedTenant in $ExpandedTenants | Where-Object { $_.value -notin $ExcludedTenants }) {

Modules/CIPPCore/Public/Webhooks/Test-CIPPAuditLogRules.ps1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,14 @@ function Test-CIPPAuditLogRules {
153153
$ExpandedTenants = Expand-CIPPTenantGroups -TenantFilter $Tenants
154154
# Check if the TenantFilter matches any tenant in the expanded list or AllTenants
155155
if ($ExpandedTenants.value -contains $TenantFilter -or $ExpandedTenants.value -contains 'AllTenants') {
156+
# Expand tenant groups in exclusions the same way as inclusions
157+
$ExcludedTenants = $ConfigEntry.excludedTenants | ConvertFrom-Json -ErrorAction SilentlyContinue
158+
if ($ExcludedTenants) {
159+
$ExcludedTenants = @(Expand-CIPPTenantGroups -TenantFilter $ExcludedTenants)
160+
}
156161
[pscustomobject]@{
157162
Tenants = $Tenants
158-
Excluded = ($ConfigEntry.excludedTenants | ConvertFrom-Json -ErrorAction SilentlyContinue)
163+
Excluded = $ExcludedTenants
159164
Conditions = $ConfigEntry.Conditions
160165
Actions = $ConfigEntry.Actions
161166
LogType = $ConfigEntry.Type

Modules/CIPPHTTP/Public/Entrypoints/HTTP Functions/Tenant/Administration/Alerts/Invoke-AddScriptedAlert.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ function Invoke-AddScriptedAlert {
2828
$AllTenantsList = Get-Tenants -IncludeErrors
2929
$computedExcluded = @($AllTenantsList.defaultDomainName | Where-Object { $_ -notin $targetDomains })
3030

31-
$existingExcluded = @()
31+
$existingEntries = @()
3232
if ($Request.Body.PSObject.Properties['excludedTenants'] -and $Request.Body.excludedTenants) {
33-
$existingExcluded = @($Request.Body.excludedTenants | ForEach-Object { $_.value ?? $_ })
33+
$existingEntries = @($Request.Body.excludedTenants)
3434
}
35+
# Keep user-picked groups as typed objects so Add-CIPPScheduledTask stores them
36+
# for runtime expansion instead of flattening them into the domain list
37+
$excludedGroupEntries = @($existingEntries | Where-Object { $_.type -eq 'Group' })
38+
$existingExcluded = @($existingEntries | Where-Object { $_.type -ne 'Group' } | ForEach-Object { $_.value ?? $_ })
3539
$mergedExcluded = @($existingExcluded + $computedExcluded) | Where-Object { $_ } | Select-Object -Unique
3640

3741
$excludedValue = @($mergedExcluded | ForEach-Object {
3842
[PSCustomObject]@{ value = $_; label = $_ }
39-
})
43+
}) + $excludedGroupEntries
4044
$Request.Body | Add-Member -MemberType NoteProperty -Name 'excludedTenants' -Value $excludedValue -Force
4145
}
4246

Modules/CIPPHTTP/Public/Entrypoints/HTTP Functions/Tenant/Administration/Alerts/Invoke-ListAlertsQueue.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ function Invoke-ListAlertsQueue {
8989
} else {
9090
$ExcludedTenants = @()
9191
}
92+
# Excluded tenant groups are stored separately as JSON — surface them as objects so the
93+
# frontend renders a single named chip and can round-trip the group on edit
94+
if ($Task.excludedTenantGroups) {
95+
$ExcludedGroups = @($Task.excludedTenantGroups | ConvertFrom-Json -ErrorAction SilentlyContinue)
96+
if ($ExcludedGroups) {
97+
$ExcludedTenants = @($ExcludedTenants + $ExcludedGroups)
98+
}
99+
}
92100

93101
# Handle tenant display information for alerts
94102
$TenantsForDisplay = @()

0 commit comments

Comments
 (0)