Skip to content

Commit f3d4329

Browse files
committed
Add alerts overview
1 parent e743495 commit f3d4329

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
function Invoke-ListAlertResults {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint,AnyTenant
5+
.ROLE
6+
CIPP.Alert.Read
7+
.DESCRIPTION
8+
Lists the currently-active fired alert items for a tenant, read from the
9+
AlertLastRun table. AlertLastRun stores the items produced by the most recent
10+
run of each scripted alert (Get-CIPPAlert*) after snoozed items have already
11+
been filtered out, so this returns the active (non-snoozed) instances. Each
12+
item is returned with a content preview/hash (matching the snooze format) and
13+
the raw alert item so the frontend can snooze it via ExecSnoozeAlert.
14+
#>
15+
[CmdletBinding()]
16+
param($Request, $TriggerMetadata)
17+
18+
$APIName = $Request.Params.CIPPEndpoint
19+
$TenantFilter = $Request.Query.tenantFilter ?? $Request.Body.tenantFilter
20+
21+
try {
22+
if ([string]::IsNullOrWhiteSpace($TenantFilter)) {
23+
return ([HttpResponseContext]@{
24+
StatusCode = [HttpStatusCode]::BadRequest
25+
Body = @{ Results = 'tenantFilter is required.' }
26+
})
27+
}
28+
29+
$Table = Get-CIPPTable -tablename 'AlertLastRun'
30+
# AlertLastRun: PartitionKey = run date (yyyyMMdd), RowKey = "{tenant}-{cmdlet}"
31+
$SafeTenant = ConvertTo-CIPPODataFilterValue -Value $TenantFilter -Type String
32+
$Rows = Get-CIPPAzDataTableEntity @Table -Filter "Tenant eq '$SafeTenant'"
33+
34+
# Keep only the most recent run (highest date partition) per alert. RowKey is
35+
# "{tenant}-{cmdlet}", uniquely identifying the alert for this tenant. Write-AlertTrace
36+
# only writes a new row when the data changes, so the latest row is the current state.
37+
$LatestByAlert = @{}
38+
foreach ($Row in @($Rows)) {
39+
$Key = $Row.RowKey
40+
$Existing = $LatestByAlert[$Key]
41+
if (-not $Existing -or [string]$Row.PartitionKey -gt [string]$Existing.PartitionKey) {
42+
$LatestByAlert[$Key] = $Row
43+
}
44+
}
45+
46+
$Results = [System.Collections.Generic.List[object]]::new()
47+
foreach ($Row in $LatestByAlert.Values) {
48+
if ([string]::IsNullOrWhiteSpace($Row.LogData)) { continue }
49+
try {
50+
$Items = $Row.LogData | ConvertFrom-Json -ErrorAction Stop
51+
} catch {
52+
Write-Information "Failed to parse AlertLastRun LogData for $($Row.RowKey): $($_.Exception.Message)"
53+
continue
54+
}
55+
56+
foreach ($Item in @($Items)) {
57+
if ($null -eq $Item) { continue }
58+
$Hash = Get-AlertContentHash -AlertItem $Item
59+
$Results.Add([PSCustomObject]@{
60+
CmdletName = $Row.CmdletName
61+
AlertComment = $Row.AlertComment
62+
Tenant = $Row.Tenant
63+
LastRun = $Row.PartitionKey
64+
ContentHash = $Hash.ContentHash
65+
ContentPreview = $Hash.ContentPreview
66+
AlertItem = $Item
67+
})
68+
}
69+
}
70+
71+
return ([HttpResponseContext]@{
72+
StatusCode = [HttpStatusCode]::OK
73+
Body = @($Results)
74+
})
75+
} catch {
76+
$ErrorMessage = Get-CippException -Exception $_
77+
Write-LogMessage -API $APIName -tenant $TenantFilter -message "Failed to list alert results: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
78+
return ([HttpResponseContext]@{
79+
StatusCode = [HttpStatusCode]::InternalServerError
80+
Body = @{ Results = "Failed to list alert results: $($ErrorMessage.NormalizedError)" }
81+
})
82+
}
83+
}

Modules/CIPPHTTP/Public/Entrypoints/HTTP Functions/CIPP/Core/Invoke-ListSnoozedAlerts.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ function Invoke-ListSnoozedAlerts {
4141
RowKey = $_.RowKey
4242
CmdletName = $_.PartitionKey
4343
Tenant = $_.Tenant
44+
ContentHash = $_.ContentHash
4445
ContentPreview = $_.ContentPreview
4546
SnoozedBy = $_.SnoozedBy
4647
SnoozedAt = $_.SnoozedAt

0 commit comments

Comments
 (0)