-1

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
Chris9834
  • 151
  • 1
  • 11

2 Answers2

0

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
Chris9834
  • 151
  • 1
  • 11
0

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