Sending Notifications to Teams using PowerShell

I thought that as so many of us are now using teams, why not send more information to a channel with results from automated PowerShell scripts.

We can do this via a webhook.

So lets setup a webhook on the Teams Channel.  We’ll need to do this via a connector.

Open the Channel and click the More Options button (which appears as three dots at the top right of the window) and Select Connectors.

Teams-Scripts-02

Scroll down to Incoming Webhook and click the Add button.

Teams-Scripts-03

Give the connector a name and image of your choosing and finally click Create.

Teams-Scripts-04

A new unique URI is automatically generated. Copy this URI string to your clipboard.

Teams-Scripts-05

Note: These details are available if you lose the url in the future.  Select the Connector and then click the Manage button to you can copy the URI string again.

Now we just want some PowerShell code to insert into the webhook, so this needs to be in JSON format.

This worked great.

Teams-Scripts-07a

But, I’d like to add more details to the notifications, like script identification, errors, etc that the scripts encountered. Message cards looked good, so I decided to try a basic message card.

Message cards can do a lot more than notification too. If you’re interested you can check out more about message cards in the references below.

Teams-Scripts-07b

I personally find this format is much better as a Teams notification as it could contain potentially a lot of information from scripts.

Reference

Message Cards
https://docs.microsoft.com/en-us/outlook/actionable-messages/card-reference
https://messagecardplayground.azurewebsites.net/