First, read this previous post that gives a primer on how to interact with Microsoft Teams.

The way to post a message to a channel using PowerShell is like this:

Invoke-RestMethod -Method post -ContentType 'Application/Json' -Body '{"text":"Hello World!"}' -Uri <YOUR WEBHOOK URL>

The challenge is your message body is almost always a complex string, and using string interpolation to construct it is a nightmare.

The solution to this is to take advantage of the fact that the message body is actually JSON.

This means you can construct the payload first, and then invoke the URI.

This allows you to perform your string manipulations first, then pass a simple object to the webhook.

$tag = "1.0.8"

$databaseBackupPath = "Dropbox"

$webhook = "https://INSERT YOUR WEBHOOK HERE"

$message = "Notification: Generated database backup of $tag to $databaseBackupPath"


$json = @"
{
    "Text": "$message",
}
"@

Invoke-RestMethod -Method post -ContentType 'Application/Json' -Body $json -Uri $webhook

You should get a success returned.

Happy hacking!