If you have tried automating posts to Teams, you may have noticed that, as of the date of writing this post, some information and documentation is limited. There is still some fairly helpful info out there but why reinvent the wheel? I figured I would toss my hat into the ring and try to help others along the way.
The following code is my "quick and dirty" Teams Webhook class, it is functional with the necessary info is given. The schema is basic and should be added to fit your needs.
I will be adding this to GitHub soon, hopefully with new features.
class TeamsWebhook
{
[System.Collections.Specialized.OrderedDictionary] static NewTeamsCard()
{
$card = [ordered]@{
title = "Add Your Title"
text = " "
sections = @(([TeamsWebhook]::GetTeamsSections()))
}
return $card
}
[System.Collections.ArrayList] static GetTeamsSections()
{
[array]$sections = New-Object System.Collections.ArrayList
$sections += [TeamsWebhook]::GetTeamsActivitySection()
$sections += [TeamsWebhook]::GetTeamsFactSection()
return $sections
}
[System.Collections.Specialized.OrderedDictionary] static GetTeamsActivitySection()
{
$section = [ordered]@{
activityTitle = 'Add Activity Title'
activitySubtitle = 'Add Subtitle'
#activityText = " "
activityImage = '.\image.png'
}
return $section
}
[System.Collections.Specialized.OrderedDictionary] static GetTeamsFactSection()
{
$section = [ordered]@{
title = 'Add Section Title'
facts = @(
@{
name = 'Test'
value = "Successful"
}
)
}
return $section
}
PostTeamsCard([string] $uri)
{
$body = ConvertTo-Json -Depth 4 -InputObject ([TeamsWebhook]::NewTeamsCard())
Invoke-RestMethod -uri $uri -Method 'Post' -body $body -ContentType 'application/json' | Out-Null
}
}