This is my first post here ,i hope i will get some help. I have created a script in powershell for checking the expiry date of services principal in azure.The script itself works fine but now i want to post the output of script to MS Teams instead of generating a result file. This is a piece of code.The result.list contains the output and i am loading it to $Body
$Body = get-content -Path .\result.list
$JSONBody = [PSCustomObject][Ordered]@{
"@type" = "text/plain"
"@context" = "http://schema.org/extensions"
"summary" = "Incoming Alert Test Message!"
"themeColor" = '0078D7'
"title" = "Incoming Alert Test Message!"
"text" = "$Body"
}
$TeamMessageBody = ConvertTo-Json $JSONBody -Depth 100
$parameters = @{
"URI" = '<uri>'
"Body" = $TeamMessageBody
"ContentType" = 'application/json'
}
Invoke-RestMethod @parameters
This i how the result.list looks like.E.g for a service-principal
DisplayName : sp-acr-ldl-pull
ObjectId : ***********
ApplicationId : ***********
KeyId : ***********
Type : Password
StartDate : 6/23/2020 2:29:20 PM
EndDate : 6/23/2021 2:29:16 PM
Status : Expired
But in Teams it is not human readable because the body is not passed correctly
DisplayName: sp-acr-ldl-pull ObjectId: ********** ApplicationId : ********** KeyId: *********** Type: Password StartDate: 6/23/2020 2:29:20PM EndDate
How can i format $JSONBody to get the same output in Teams same in result.list?
CodePudding user response:
Basically, the problem is the line breaks, but you've got two related problems:
You're reading the contents of the file directly using
Get-Content
. By default in PowerShell this will return not a single string but rather an array of strings, one item for each line in the file. To get the contents as a single raw string, add the-Raw
parameter at the end of the command.By default, Teams is simply displaying your text without any formatting (including line breaks). If you want to send explicit line breaks, you need to tell Teams to do so, and you can do this using html "
" tags (Teams message bodies support a limited subset of html). To do this, you can replace the line breaks in the$body
variable with html<br>
tags, like this:$Body = $Body.Replace("`r`n", "<br>")
.
Note that you're missing the "Method" parameter on your Invoke-RestMethod
command.
Here is final working code, including the above:
$Body = get-content -Path "C:\temp\result.list" -Raw
$Body = $Body.Replace("`r`n", "<br>")
$JSONBody = [PSCustomObject][Ordered]@{
"@type" = "text/plain"
"@context" = "http://schema.org/extensions"
"summary" = "Incoming Alert Test Message!"
"themeColor" = '0078D7'
"title" = "Incoming Alert Test Message!"
"text" = "$Body"
}
$TeamMessageBody = ConvertTo-Json $JSONBody -Depth 100
$parameters = @{
"URI" = '<uri>'
"Body" = $TeamMessageBody
"ContentType" = 'application/json'
"Method" = "POST"
}
Invoke-RestMethod @parameters
So that will at least give you line breaks. If you want to preserve the "Table" style formatting, then I'd suggest using Adaptive Cards rather, perhaps something like the FactSet
. You can learn how to send Adaptive Cards here, and here is a sample card using a FactSet, but you'd need to loop through the lines and build up the FactSet and the Adaptive Card, so hopefully the earlier solution is sufficient.
CodePudding user response:
with or without html it seems to be a display problem with Teams.
This is what i get in Teams: [1]: https://i.stack.imgur.com/GKNbP.png The same content if i copy/paste from Teams to notepad,then i get the desired result:
DisplayName : sp-acr-ldl-pull
ObjectId : *
ApplicationId : *
KeyId : *
Type : Password
StartDate : 6/23/2020 2:29:20 PM
EndDate : 6/23/2021 2:29:16 PM
Status : Expired
DisplayName : sp-acr-ldl-pull
ObjectId : *
ApplicationId : *
KeyId : *
Type : Password
StartDate : 4/14/2020 9:41:03 AM
EndDate : 4/14/2021 9:41:02 AM
Status : Expired
Not sure if i can do something to solve this display issue.