|
| 1 | +using namespace System.Net |
| 2 | + |
| 3 | +function Invoke-ExecMDOAlertsList { |
| 4 | + <# |
| 5 | + .FUNCTIONALITY |
| 6 | + Entrypoint |
| 7 | + .ROLE |
| 8 | + Security.Alert.Read |
| 9 | + #> |
| 10 | + [CmdletBinding()] |
| 11 | + param($Request, $TriggerMetadata) |
| 12 | + |
| 13 | + $APIName = $Request.Params.CIPPEndpoint |
| 14 | + $Headers = $Request.Headers |
| 15 | + Write-LogMessage -headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug' |
| 16 | + |
| 17 | + # Interact with query parameters or the body of the request. |
| 18 | + $TenantFilter = $Request.Query.tenantFilter |
| 19 | + |
| 20 | + try { |
| 21 | + $GraphRequest = if ($TenantFilter -ne 'AllTenants') { |
| 22 | + # Single tenant functionality |
| 23 | + New-GraphGetRequest -uri "https://graph.microsoft.com/beta/security/alerts_v2?`$filter=serviceSource eq 'microsoftDefenderForOffice365'" -tenantid $TenantFilter |
| 24 | + } else { |
| 25 | + # AllTenants functionality |
| 26 | + $Table = Get-CIPPTable -TableName cachealertsandincidents |
| 27 | + $PartitionKey = 'MdoAlert' |
| 28 | + $Filter = "PartitionKey eq '$PartitionKey'" |
| 29 | + $Rows = Get-CIPPAzDataTableEntity @Table -filter $Filter | Where-Object -Property Timestamp -GT (Get-Date).AddMinutes(-30) |
| 30 | + $QueueReference = '{0}-{1}' -f $TenantFilter, $PartitionKey |
| 31 | + $RunningQueue = Invoke-ListCippQueue -Reference $QueueReference | Where-Object { $_.Status -notmatch 'Completed' -and $_.Status -notmatch 'Failed' } |
| 32 | + # If a queue is running, we will not start a new one |
| 33 | + if ($RunningQueue) { |
| 34 | + $Metadata = [PSCustomObject]@{ |
| 35 | + QueueMessage = 'Still loading data for all tenants. Please check back in a few more minutes' |
| 36 | + QueueId = $RunningQueue.RowKey |
| 37 | + } |
| 38 | + } elseif (!$Rows -and !$RunningQueue) { |
| 39 | + # If no rows are found and no queue is running, we will start a new one |
| 40 | + $TenantList = Get-Tenants -IncludeErrors |
| 41 | + $Queue = New-CippQueueEntry -Name 'MDO Alerts - All Tenants' -Link '/security/reports/mdo-alerts?customerId=AllTenants' -Reference $QueueReference -TotalTasks ($TenantList | Measure-Object).Count |
| 42 | + $Metadata = [PSCustomObject]@{ |
| 43 | + QueueMessage = 'Loading data for all tenants. Please check back in a few minutes' |
| 44 | + QueueId = $Queue.RowKey |
| 45 | + } |
| 46 | + $InputObject = [PSCustomObject]@{ |
| 47 | + OrchestratorName = 'MdoAlertsOrchestrator' |
| 48 | + QueueFunction = @{ |
| 49 | + FunctionName = 'GetTenants' |
| 50 | + QueueId = $Queue.RowKey |
| 51 | + TenantParams = @{ |
| 52 | + IncludeErrors = $true |
| 53 | + } |
| 54 | + DurableName = 'ExecMdoAlertsListAllTenants' |
| 55 | + } |
| 56 | + SkipLog = $true |
| 57 | + } |
| 58 | + Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($InputObject | ConvertTo-Json -Depth 5 -Compress) | Out-Null |
| 59 | + } else { |
| 60 | + $Metadata = [PSCustomObject]@{ |
| 61 | + QueueId = $RunningQueue.RowKey ?? $null |
| 62 | + } |
| 63 | + $Alerts = $Rows |
| 64 | + foreach ($alert in $Alerts) { |
| 65 | + ConvertFrom-Json -InputObject $alert.MdoAlert -Depth 10 |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } catch { |
| 70 | + $Body = Get-NormalizedError -Message $_.Exception.Message |
| 71 | + $StatusCode = [HttpStatusCode]::Forbidden |
| 72 | + } |
| 73 | + if (!$Body) { |
| 74 | + $StatusCode = [HttpStatusCode]::OK |
| 75 | + $Body = [PSCustomObject]@{ |
| 76 | + Results = @($GraphRequest) |
| 77 | + Metadata = $Metadata |
| 78 | + } |
| 79 | + } |
| 80 | + Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ |
| 81 | + StatusCode = $StatusCode |
| 82 | + Body = $Body |
| 83 | + }) |
| 84 | +} |
0 commit comments