Skip to content

Commit 7a33197

Browse files
committed
fix json body for webhooks
1 parent 115ab34 commit 7a33197

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

Modules/CIPPCore/Public/Send-CIPPAlert.ps1

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,35 +112,41 @@ function Send-CIPPAlert {
112112
$Headers = $null
113113
}
114114

115-
$JSONBody = Get-CIPPTextReplacement -TenantFilter $TenantFilter -Text $JSONContent -EscapeForJson
115+
$ReplacedContent = Get-CIPPTextReplacement -TenantFilter $TenantFilter -Text $JSONContent -EscapeForJson
116116
try {
117117
if (![string]::IsNullOrWhiteSpace($Config.webhook) -or ![string]::IsNullOrWhiteSpace($AltWebhook)) {
118118
if ($PSCmdlet.ShouldProcess($Config.webhook, 'Sending webhook')) {
119119
$webhook = if ($AltWebhook) { $AltWebhook } else { $Config.webhook }
120120
switch -wildcard ($webhook) {
121121
'*webhook.office.com*' {
122-
$JSONBody = "{`"text`": `"You've setup your alert policies to be alerted whenever specific events happen. We've found some of these events in the log. <br><br>$JSONContent`"}"
123-
Invoke-RestMethod -Uri $webhook -Method POST -ContentType 'Application/json' -Body $JSONBody
122+
$TeamsBody = [PSCustomObject]@{
123+
text = "You've setup your alert policies to be alerted whenever specific events happen. We've found some of these events in the log. <br><br>$ReplacedContent"
124+
} | ConvertTo-Json -Compress
125+
Invoke-RestMethod -Uri $webhook -Method POST -ContentType 'Application/json' -Body $TeamsBody
124126
}
125127
'*discord.com*' {
126-
$JSONBody = "{`"content`": `"You've setup your alert policies to be alerted whenever specific events happen. We've found some of these events in the log. $JSONContent`"}"
127-
Invoke-RestMethod -Uri $webhook -Method POST -ContentType 'Application/json' -Body $JSONBody
128+
$DiscordBody = [PSCustomObject]@{
129+
content = "You've setup your alert policies to be alerted whenever specific events happen. We've found some of these events in the log. ``````$ReplacedContent``````"
130+
} | ConvertTo-Json -Compress
131+
Invoke-RestMethod -Uri $webhook -Method POST -ContentType 'Application/json' -Body $DiscordBody
128132
}
129133
'*slack.com*' {
130134
$SlackBlocks = Get-SlackAlertBlocks -JSONBody $JSONContent
131135
if ($SlackBlocks.blocks) {
132-
$JSONBody = $SlackBlocks | ConvertTo-Json -Depth 10 -Compress
136+
$SlackBody = $SlackBlocks | ConvertTo-Json -Depth 10 -Compress
133137
} else {
134-
$JSONBody = "{`"text`": `"You've setup your alert policies to be alerted whenever specific events happen. We've found some of these events in the log. $JSONContent`"}"
138+
$SlackBody = [PSCustomObject]@{
139+
text = "You've setup your alert policies to be alerted whenever specific events happen. We've found some of these events in the log. ``````$ReplacedContent``````"
140+
} | ConvertTo-Json -Compress
135141
}
136-
Invoke-RestMethod -Uri $webhook -Method POST -ContentType 'Application/json' -Body $JSONBody
142+
Invoke-RestMethod -Uri $webhook -Method POST -ContentType 'Application/json' -Body $SlackBody
137143
}
138144
default {
139145
$RestMethod = @{
140146
Uri = $webhook
141147
Method = 'POST'
142148
ContentType = 'application/json'
143-
Body = $JSONContent
149+
Body = $ReplacedContent
144150
}
145151
if ($Headers) {
146152
$RestMethod['Headers'] = $Headers

0 commit comments

Comments
 (0)