PowerShell Script to Post a Message to a Microsoft Teams Channel

Introduction

Our PowerShell Sample Code shows how-to post a Message to a Microsoft Teams Channel.
We’ve also included an example JSON-Object , and an example Message which would be posted to the Microsoft Teams Channel.
The below examples clearly show which JSON Field is used where within the posted Message.

We’ve tried to keep the code as simple as possible, whilst trying to show all possible features.

Example JSON-Object


{
    "summary":  "Summary",
    "title":  "Title",
    "sections":  [
                     {
                         "activitySubTitle":  "Section1.ActivitySubTitle",
                         "activityImage":  "https://thomas.geens.be/wp-content/uploads/2019/02/favicon-1.gif",
                         "facts":  [
                                       {
                                           "value":  "Section1.Fact2.Value",
                                           "name":  "Section1.Fact1.Name"
                                       },
                                       {
                                           "value":  "Section1.Fact.2Value",
                                           "name":  "Section1.Fact2.Name"
                                       }
                                   ],
                         "activityTitle":  "Section1.Activitytitle",
                         "activityText":  "Section1.ActivityText"
                     },
                     ...
                ],
    "potentialAction":  [
                            {
                                "@context":  "http://schema.org",
                                "name":  "Google",
                                "target":  [
                                               "https://www.google.be"
                                           ],
                                "@type":  "ViewAction"
                            },
                            {
                                "@context":  "http://schema.org",
                                "name":  "Wikipedia",
                                "target":  [
                                               "https://www.wikipedia.com"
                                           ],
                                "@type":  "ViewAction"
                            }
                        ],
    "text":  "Text"
}

Example Message

Example Message posted to a Microsoft Teams Channel by PowerShell

Requirements

To be able to post to a Microsoft Teams Channel you first need to set-up a WebHook Connector. This connector will provide you with an URI, which you will later use to publish your JSON-Object to using an HTTP POST Request.

  1. Click on the ‘…’ link next to the Channel within a Team to open up the Channel-menu
  2. Select the ‘Connectors’ option to manage the Channel’s Connectors


  3. Add a Connector type ‘Incoming Webhook’
    undefined

  4. Fill in the necessary information and click the ‘Create’ button
    undefined

  5. Copy the Webhook URI by clicking the ‘Copy-icon’, you will need this later in your script, so keep it safe
  6. You may close the pop-up window by clicking the ‘Done’ button
    undefined

Script

Now that you’ve set-up your WebHook Connector you’re able to use the below script for testing.


#--- Insert your Webhook Connector URI
$uri = '###INSERT YOUR WEBHOOK CONNECTOR URI###'
#--- Insert a public URI to the Image you want to attach to the Message
$image = 'https://thomas.geens.be/wp-content/uploads/2019/02/favicon-1.gif'

# Create a Generic List holding the Activities
$Sections = New-Object 'System.Collections.Generic.List[System.Object]'

# Insert 3 Different Activities as an example 
for ($i = 1; $i -lt 4; $i++) {
    # Generate Activity Number 1-3
    $Section = @{
        activityTitle = "Section$i.Activitytitle"
        activitySubTitle = "Section$i.ActivitySubTitle"
        activityText = "Section$i.ActivityText"
        activityImage = $image
        facts = @(
                    @{
                        name = "Section$i.Fact1.Name"
                        value = "Section$i.Fact2.Value"
                    },
                    @{
                        name = "Section$i.Fact2.Name"
                        value = "Section$i.Fact.2Value"
                    }
                )  
    }
    # Add Generated Activity Number 1-3 To Sections
    $Sections.Add($Section)
}

# Create the Message holding the Activities inside
# Convert Message Generic List Object to a JSON Array Object
$body = ConvertTo-Json -Depth 8 @{
    title = 'Title'
    text = 'Text'
    summary = 'Summary'
    sections = $Sections
    potentialAction =   @(
                            @{
                                '@context'  = 'http://schema.org'
                                '@type' = 'ViewAction'
                                name = 'Google'
                                target = @("https://www.google.be")
                            },
                            @{
                                '@context'  = 'http://schema.org'
                                '@type' = 'ViewAction'
                                name = 'Wikipedia'
                                target = @("https://www.wikipedia.com")
                            }
                        ) 
}

# Post the JSON Array Object to the Webhook Connector URI
Invoke-RestMethod -Uri $uri -Method Post -Body $body -ContentType 'application/json'

Thomas Geens:

Happy Coding to you Guys!