Skip to content

Commit

Permalink
added severity handling to alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
KelvinTegelaar committed Aug 8, 2023
1 parent c111750 commit 5b296b9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
6 changes: 2 additions & 4 deletions ExecNotificationConfig/run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -m

# Write to the Azure Functions log stream.
Write-Host 'PowerShell HTTP trigger function processed a request.'
$sev = ([pscustomobject]$Request.body.Severity).value -join (',')
$results = try {
$Table = Get-CIPPTable -TableName SchedulerConfig
$SchedulerConfig = @{
'tenant' = 'Any'
'tenantid' = 'TenantId'
'type' = 'CIPPNotifications'
'schedule' = 'Every 15 minutes'
'Severity' = [string]$sev
'email' = "$($Request.Body.Email)"
'webhook' = "$($Request.Body.Webhook)"
'onePerTenant' = [boolean]$Request.Body.onePerTenant
Expand All @@ -26,10 +28,6 @@ $results = try {
foreach ($logvalue in [pscustomobject]$Request.body.logsToInclude) {
$SchedulerConfig[([pscustomobject]$logvalue.value)] = $true
}
foreach ($severityvalue in [pscustomobject]$Request.body.Severity) {
$SchedulerConfig[([pscustomobject]$severityvalue.value)] = $true
}


Add-AzDataTableEntity @Table -Entity $SchedulerConfig -Force | Out-Null
'Successfully set the configuration'
Expand Down
7 changes: 4 additions & 3 deletions ListNotificationConfig/run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ $Table = Get-CIPPTable -TableName SchedulerConfig
$Filter = "RowKey eq 'CippNotifications' and PartitionKey eq 'CippNotifications'"
$Config = Get-AzDataTableEntity @Table -Filter $Filter | ConvertTo-Json -Depth 10 | ConvertFrom-Json -Depth 10
$config | Add-Member -NotePropertyValue @() -NotePropertyName 'logsToInclude' -Force
$config | Add-Member -NotePropertyValue @() -NotePropertyName 'Severity' -Force
$config.logsToInclude = @(([pscustomobject]$config | Select-Object * -ExcludeProperty schedule, type, tenantid, onepertenant, sendtoIntegration, partitionkey, rowkey, tenant, ETag, email, logsToInclude, Severity, Alert, Info, Error, timestamp, webhook).psobject.properties.name)
if (!$config.logsToInclude) {
$config.logsToInclude = @('None')
}
$config.Severity = @(([pscustomobject]$config | Select-Object * -ExcludeProperty schedule, type, tenantid, onepertenant, sendtoIntegration, partitionkey, rowkey, tenant, ETag, email, logsToInclude, Severity, Updates, Standards, TokensUpdater, ExecExcludeLicenses, ExecExcludeTenant, ExecDnsConfig, EditUser, ChocoApp, AddAPDevice, EditTenant, AddMSPApp, AddUser, timestamp, AddGroup, ExecOffboardUser, webhook).psobject.properties.name)
if (!$config.Severity) {
$config.Severity = @('None')
$config.Severity = @('Alert')
}
else {
$config.Severity = $config.Severity -split ','
}
$body = $Config

Expand Down
17 changes: 11 additions & 6 deletions Scheduler_CIPPNotifications/run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ $Config = [pscustomobject](Get-AzDataTableEntity @Table -Filter $Filter)

$Settings = [System.Collections.ArrayList]@('Alerts')
$Config.psobject.properties.name | ForEach-Object { $settings.add($_) }

$severity = $Config.Severity -split ','
if (!$severity) {
$severity = [System.Collections.ArrayList]@('Info', 'Error', 'Warning', 'Critical', 'Alert')
}
$Table = Get-CIPPTable
$PartitionKey = Get-Date -UFormat '%Y%m%d'
$Filter = "PartitionKey eq '{0}'" -f $PartitionKey
$Currentlog = Get-AzDataTableEntity @Table -Filter $Filter | Where-Object { $_.API -In $Settings -and $_.SentAsAlert -ne $true }
$PartitionKey = Get-Date -UFormat '% Y%m%d'
$Filter = "PartitionKey eq '{ 0 }'" -f $PartitionKey
$Currentlog = Get-AzDataTableEntity @Table -Filter $Filter | Where-Object {
$_.API -In $Settings -and $_.SentAsAlert -ne $true -and $_.Severity -In $severity
}

#email try
try {
if ($config.onePerTenant) {
if ($Config.email -like '*@*' -and $null -ne $CurrentLog) {
$JSONRecipients = $Config.email.split(",").trim() | ForEach-Object { if ($_ -like '*@*') { '{"EmailAddress": {"Address": "' + $_ + '"}},' } }
$JSONRecipients = $Config.email.split(",").trim() | ForEach-Object { if ($_ -like '*@*') { '{ "EmailAddress": { "Address": "' + $_ + '" } }, ' } }
$JSONRecipients = ([string]$JSONRecipients).Substring(0, ([string]$JSONRecipients).Length - 1)
foreach ($tenant in ($CurrentLog.Tenant | Sort-Object -Unique)) {
$HTMLLog = ($CurrentLog | Select-Object Message, API, Tenant, Username, Severity | Where-Object -Property tenant -EQ $tenant | ConvertTo-Html -frag) -replace '<table>', '<table class=blueTable>' | Out-String
Expand Down Expand Up @@ -47,7 +52,7 @@ try {
}
else {
if ($Config.email -like '*@*' -and $null -ne $CurrentLog) {
$JSONRecipients = $Config.email.split(",").trim() | ForEach-Object { if ($_ -like '*@*') { '{"EmailAddress": {"Address": "' + $_ + '"}},' } }
$JSONRecipients = $Config.email.split(",").trim() | ForEach-Object { if ($_ -like '*@*') { '{ "EmailAddress": { "Address": "' + $_ + '" } }, ' } }
$JSONRecipients = ([string]$JSONRecipients).Substring(0, ([string]$JSONRecipients).Length - 1)
$HTMLLog = ($CurrentLog | Select-Object Message, API, Tenant, Username, Severity | ConvertTo-Html -frag) -replace '<table>', '<table class=blueTable>' | Out-String
$JSONBody = @"
Expand Down

0 comments on commit 5b296b9

Please sign in to comment.