I can easily send a message through to teams with this code in Powershell:
Invoke-RestMethod -Method post -ContentType 'Application/Json' -Body '{"text":"Hello World!"}' -Uri <YOUR WEBHOOK URL>
Now I'd like to instead send a variable through, but no matter what I try, it either sends what I have in it as plain text or I get the error: Invoke-RestMethod : Bad payload received by generic incoming webhook.
Here are some of the ways I've tried this:
Invoke-RestMethod -Method post -ContentType 'Application/Json' -Body '{"text":"Write-Output $cmdOutput"}' -Uri <YOUR WEBHOOK URL>
Invoke-RestMethod -Method post -ContentType 'Application/Json' -Body $cmdOutput -Uri <YOUR WEBHOOK URL>
Invoke-RestMethod -Method post -ContentType 'Application/Json' -Body Write-Output $cmdOutput -Uri <YOUR WEBHOOK URL>
The first example sends: Write-Output $cmdOutput, the other two get the error I've put above.
What am I missing? Is there a way to turn the variable to text before passing it through to teams?
I am using this for documentation: Send messages using cURL and PowerShell