Send-MSTTeamMessage
Teams: Publish a message in a Microsoft Teams channel via webhook
#Requires -Version 5.1
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[ValidatePattern('^https://outlook.office.com/webhook/')]
[string]$WebhookURL,
[Parameter(Mandatory = $true)]
[string]$Message,
[string]$Title,
[ValidateSet('Orange', 'Green', 'Red')]
[string]$MessageColor,
[string]$ActivityTitle,
[string]$ActivitySubtitle
)
Process {
try {
$body = [PSCustomObject]@{
text = $Message
title = $Title
themeColor = if ($MessageColor -eq 'Orange') { '#FFA500' } elseif ($MessageColor -eq 'Green') { '#008000' } elseif ($MessageColor -eq 'Red') { '#FF0000' } else { '' }
}
$jsonBody = $body | ConvertTo-Json -Depth 3
$null = Invoke-RestMethod -Uri $WebhookURL -Method Post -Body $jsonBody -ContentType 'application/json' -ErrorAction Stop
[PSCustomObject]@{
Timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
Status = 'Message sent to Microsoft Teams'
Title = $Title
}
}
catch { throw }
}The URL of the incoming webhook, must start with "https://outlook.office.com/webhook/"
The body of the message to publish on Teams
The title of the message to publish on Teams
The color theme for the message (Orange, Green, Red)
The Activity title of the message
The Activity subtitle of the message