- Secure domain controllers with LDAP channel binding and LDAP signing - Tue, Jul 13 2021
- Use case for action cards: Send low storage space alert to Microsoft Teams and start WAC to fix the problem - Mon, Jun 21 2021
- Move or migrate WSUS to a new server - Wed, Dec 16 2020
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.
The following dialog box offers a button to add a webhook to a team or channel.
In the next step, the assistant prompts you to select a team or channel.
Before we create the webhook, we give it a name and optionally upload an icon for it.
After we have clicked on Create, we will receive the link to which we can send the notifications using PowerShell or other tools.
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.
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!
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.
I think the schema is missing quotes in code block. <3 you guys!!!
William, thanks for the hint! We fixed it.
is there a solution to use the webhook but instead send to a team, can it send to a specific user?
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