|
| 1 | +function Invoke-AddScriptedAlert { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Entrypoint |
| 5 | + .ROLE |
| 6 | + CIPP.Alert.ReadWrite |
| 7 | + #> |
| 8 | + [CmdletBinding()] |
| 9 | + param($Request, $TriggerMetadata) |
| 10 | + |
| 11 | + $tenantsJsonForStorage = $null |
| 12 | + |
| 13 | + if ($Request.Body.tenantFilter -is [array] -and @($Request.Body.tenantFilter).Count -eq 1) { |
| 14 | + $Request.Body | Add-Member -MemberType NoteProperty -Name 'tenantFilter' -Value $Request.Body.tenantFilter[0] -Force |
| 15 | + } |
| 16 | + |
| 17 | + if ($Request.Body.tenantFilter -is [array] -and @($Request.Body.tenantFilter).Count -gt 1) { |
| 18 | + try { |
| 19 | + $originalSelection = @($Request.Body.tenantFilter) |
| 20 | + $tenantsJsonForStorage = $originalSelection | ConvertTo-Json -Compress -Depth 10 |
| 21 | + |
| 22 | + $hasAllTenants = @($originalSelection | Where-Object { $_.value -eq 'AllTenants' }).Count -gt 0 |
| 23 | + |
| 24 | + if (-not $hasAllTenants) { |
| 25 | + $ExpandedSelection = Expand-CIPPTenantGroups -TenantFilter $originalSelection |
| 26 | + $targetDomains = @($ExpandedSelection | ForEach-Object { $_.value }) |
| 27 | + |
| 28 | + $AllTenantsList = Get-Tenants -IncludeErrors |
| 29 | + $computedExcluded = @($AllTenantsList.defaultDomainName | Where-Object { $_ -notin $targetDomains }) |
| 30 | + |
| 31 | + $existingExcluded = @() |
| 32 | + if ($Request.Body.PSObject.Properties['excludedTenants'] -and $Request.Body.excludedTenants) { |
| 33 | + $existingExcluded = @($Request.Body.excludedTenants | ForEach-Object { $_.value ?? $_ }) |
| 34 | + } |
| 35 | + $mergedExcluded = @($existingExcluded + $computedExcluded) | Where-Object { $_ } | Select-Object -Unique |
| 36 | + |
| 37 | + $excludedValue = @($mergedExcluded | ForEach-Object { |
| 38 | + [PSCustomObject]@{ value = $_; label = $_ } |
| 39 | + }) |
| 40 | + $Request.Body | Add-Member -MemberType NoteProperty -Name 'excludedTenants' -Value $excludedValue -Force |
| 41 | + } |
| 42 | + |
| 43 | + if (-not $Request.Body.PSObject.Properties['RowKey'] -or -not $Request.Body.RowKey) { |
| 44 | + $Request.Body | Add-Member -MemberType NoteProperty -Name 'RowKey' -Value ((New-Guid).Guid) -Force |
| 45 | + } |
| 46 | + |
| 47 | + $tenantFilterValue = [PSCustomObject]@{ |
| 48 | + value = 'AllTenants' |
| 49 | + label = '*All Tenants' |
| 50 | + type = 'Tenant' |
| 51 | + } |
| 52 | + $Request.Body | Add-Member -MemberType NoteProperty -Name 'tenantFilter' -Value $tenantFilterValue -Force |
| 53 | + } catch { |
| 54 | + Write-Warning "Failed to process multi-tenant alert selection: $($_.Exception.Message)" |
| 55 | + $tenantsJsonForStorage = $null |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + $ForwardRequest = @{ |
| 60 | + Query = @{ hidden = 'true' } |
| 61 | + Body = $Request.Body |
| 62 | + Headers = $Request.Headers |
| 63 | + } |
| 64 | + $Response = Invoke-AddScheduledItem -Request $ForwardRequest -TriggerMetadata $TriggerMetadata |
| 65 | + |
| 66 | + if ($tenantsJsonForStorage) { |
| 67 | + try { |
| 68 | + $Table = Get-CIPPTable -TableName 'ScheduledTasks' |
| 69 | + $null = Update-AzDataTableEntity -Force @Table -Entity @{ |
| 70 | + PartitionKey = 'ScheduledTask' |
| 71 | + RowKey = [string]$Request.Body.RowKey |
| 72 | + Tenants = [string]$tenantsJsonForStorage |
| 73 | + } |
| 74 | + } catch { |
| 75 | + Write-Warning "Failed to persist multi-tenant selection for alert: $($_.Exception.Message)" |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return $Response |
| 80 | +} |
0 commit comments