It is a common practice for admins to receive alerts from monitoring tools or critical system messages via email or SMS. As an alternative, such notifications can be fed into Microsoft Teams to make their processing more efficient. This tutorial shows how to import data to Teams using PowerShell.

If high-priority events occur, most IT admins receive an SMS on their business cell phone and emails about error messages that are triggered by the events. However, this procedure has serious disadvantages.

Experience has shown that in large corporations, the inbox of some admins can quickly fill up. For this reason, they usually create rules to sort the messages directly into specific folders, where individual warnings might get missed.

Under these conditions, and especially when working from home, it becomes difficult to coordinate with colleagues about who will take care of which incidents. Therefore, it makes sense to collect the messages in a collaboration system such as Microsoft Teams and discuss the responsibilities there.

Webhooks serve as a mechanism for transporting the alerts to Teams. Ultimately, this is a REST API to which you pass the data in a predefined format, usually XML or JSON.

Most alerting systems today are capable of sending messages to such an interface. In our example, we will show how to create webhooks using PowerShell and Microsoft Teams.

Setting up Webhooks in Teams ^

In order to receive webhooks in Teams, we need a team and a channel that will receive the notifications. It is advisable to use separate channels for different alert sources. This way, each team member can monitor the relevant channels, activate notifications, and hide less important channels.

After we have created our own channel, which uses the name PowerShell Webhooks in our example, we open the list of apps by clicking on the corresponding icon in the navigation bar on the lower left. There, we search for "Webhooks" and select Incoming Webhook from the search results.

Select Incoming Webhook from the list of Team apps

Select Incoming Webhook from the list of Team apps

The following dialog box offers a button to add a webhook to a team or channel.

The Add to a team button is used to add the webhook to a team or channel

The Add to a team button is used to add the webhook to a team or channel

In the next step, the assistant prompts you to select a team or channel.

Select a team or channel for the webhook

Select a team or channel for the webhook

Before we create the webhook, we give it a name and optionally upload an icon for it.

Enter a name for the webhook and upload your own symbol if desired

Enter a name for the webhook and upload your own symbol if desired

After we have clicked on Create, we will receive the link to which we can send the notifications using PowerShell or other tools.

Finally the wizard generates the URL to access the channel

Finally the wizard generates the URL to access the channel

The configuration on the Teams side is now complete. Below, we will take a look at the PowerShell code.

Sending webhooks via PowerShell ^

If PowerShell is running automated tasks or collecting reports, it is a good idea to send the alarms or other information to the Teams channel at the end of the script. Parameters are transmitted in JSON format, which Teams then parses to display the text in a specific formatting, color, or type.

A simple webhook could look like this:

[String]$var = "This is a sample content"
$JSONBody = [PSCustomObject][Ordered]@{
    "@type"      = "MessageCard"
    "@context"   = "http://schema.org/extensions"
    "summary"    = "My first alert summary!"
    "themeColor" = '0078D7'
    "title"      = "My first alert."
    "text"       = "Add detailed description of the alert here!
                         You can also use variables: $var"
}
$TeamMessageBody = ConvertTo-Json $JSONBody -Depth 100
 
$parameters = @{
    "URI"         = '<webhook-URL>'
    "Method"      = 'POST'
    "Body"        = $TeamMessageBody
    "ContentType" = 'application/json'
}
Invoke-RestMethod @parameters

'In line 1, a variable is declared and initialized, which we will later include in the webhook.

Lines 2–10 contain the content and type of message. These are then converted to JSON format in line 12.

Lines 14–19 define the parameters for the HTTP request. Here, we specify the URI, which Teams generated for us when creating the connector. In addition, we attach the JSON data as Body.

With Invoke-RestMethod we finally send the whole thing off. The PowerShell console should now output a 1 as the return value if the process is successful.

An example code for a webhook in PowerShell

An example code for a webhook in PowerShell

Finally, we will receive the message card in our Teams channel. There, team members can comment directly if they want to deal with the alert or leave suggestions for solutions.

Subscribe to 4sysops newsletter!

A successful transfer of the first alert into the Teams channel

A successful transfer of the first alert into the Teams channel

Conclusion ^

With this method, you can quickly and conveniently add a Teams notification to scripts and possibly avoid mass mail notifications altogether. Nevertheless, a backup solution should be considered, because if either Microsoft 365 or Teams fails, then you will no longer receive an alert.

avatar
4 Comments
  1. William 12 months ago

    I think the schema is missing quotes in code block.   <3 you guys!!!  

  2. Robin 8 months ago

    is there a solution to use the webhook but instead send to a team, can it send to a specific user?

  3. Rey 7 months ago

    Is there a way to send alert Queryresults in the webhook , I tired using {"IncludeSearchResults": true}
    in payload message card to Teams with no success?

    Rey

Leave a reply to William Click here to cancel the reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account