Score:-1

Powershell sending to Teams: Bad payload received by generic incoming webhook

id flag

I try to send from within Powershell 7 (on Windows) to a Teams Webhook.

If i send a formatted string, it works. If i send the same string via a variable, it brakes. Any idea?

PS Microsoft.PowerShell.Core\FileSystem::\AD>  $body = "`'{`"text`":`"mailbody`"}`'" 
PS Microsoft.PowerShell.Core\FileSystem::\AD> $boDY
'{"text":"mailbody"}'
PS Microsoft.PowerShell.Core\FileSystem::\AD> Invoke-RestMethod -Method Post -ContentType 'Application/Json' -Body $body -Uri $mychat
Invoke-RestMethod: Bad payload received by generic incoming webhook.
PS Microsoft.PowerShell.Core\FileSystem::\AD> Invoke-RestMethod -Method Post -ContentType 'Application/Json' -Body '{"text":"mailbody"}' -Uri $mychat
1
Score:0
mh flag

You should create a splatting operation to save your variables.

$body = @{
    text = "mailbody"
}
Invoke-RestMethod -Method Post -ContentType 'Application/Json' -Body ($body|ConvertTo-Json) -Uri $mychat
Score:0
id flag

OK, sometimes, "stehe ich auf dem Schlauch" :-(

OK, the solution is easy. If you give the text via a variable, you do not need the starting and ending ' that defines the string. Just pass the string itself via the variable.

PS Microsoft.PowerShell.Core\FileSystem::\AD>  $body = "{`"text`":`"mailbody`"}"
PS Microsoft.PowerShell.Core\FileSystem::\AD> $body
{"text":"mailbody"}
PS Microsoft.PowerShell.Core\FileSystem::\AD> Invoke-RestMethod -Method Post -ContentType 'Application/Json' -Body $body -Uri $mychat
1
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.