MS Teams 警报,通过 webhook 和 PS 脚本触发,不想解析多行文本

MS Teams alert, triggered via webhook and PS script, does not want to parse the text on multiple lines

我正在尝试创建一个 PS 脚本,该脚本应该 post MS Teams 通过 webhooks 提醒一些指标。我几乎已经完成的当前解决方案是通过 PSCustomObject,然后将其转换为 JSON 并用作警报的 body。以下是我当前使用的代码:

$JSONBody = [PSCustomObject][Ordered] @{
     "@type"     = "MessageCard"
     "title"     = "Alert Title"
     "text"      = "Alert 1: $alert1CountVariable
                    Alert 2: $alert2CountVariable
                    Alert 3: $alert3CountVariable"
}

$TeamsMessageBody = ConvertTo-Json $JSONBody -Depth 100

$parameters = @{
    "URI"          = '<Teams Webhook URI>'
    "Method"       = 'POST'
    "Body"         = $TeamsMessageBody
    "ContentType"  = 'application/json'
}

Invoke-RestMethod @parameters

一切都按需要工作,但正如您所见,PSCustomObject 中的文本参数应该在 3 个单独的行上解析 3 个警报,无论我尝试什么,这似乎都没有发生。我尝试插入 \n 和 \r 运算符(也尝试过 \n 和 \r),但到目前为止没有任何效果。

我的另一种工作方法如下:

$Body = '{"text": "Alert 1: ' + $alert1CountVariable +' Alert 2: ' + $alert2CountVariable +' Alert 3: ' + $alert3CountVariable +'"}'
$TeamsUrl = '<Teams Webhook URI>'
Invoke-WebRequest -Uri $TeamsUrl -Method Post -Body $Body -ContentType "application/json"

然而,这也不完全满足标准,因为它仍然没有在单独的行中显示提示,并且此处没有标题。

关于如何使这两个选项中的任何一个起作用的任何建议?

团队界面似乎接受 HTML:

请查看下面调整后的脚本:

$JSONBody = [PSCustomObject][Ordered] @{
     "@type"     = "MessageCard"
     "title"     = "Alert Title"
     "text"      = "Alert 1: $alert1CountVariable <br>
                    Alert 2: $alert2CountVariable <br>
                    Alert 3: $alert3CountVariable"
}

$TeamsMessageBody = ConvertTo-Json $JSONBody -Depth 100

$parameters = @{
    "URI"          = '<Teams Webhook URI>'
    "Method"       = 'POST'
    "Body"         = $TeamsMessageBody
    "ContentType"  = 'application/json'
}

Invoke-RestMethod @parameters

我在每一行的末尾插入了 <br>,这是 HTML 中的一个换行符。