Skip to the main content.

ScriptRunner Blog

Teams-Webhooks via PowerShell – Modern Alerting (part 2/3)

Table of Content

Post Featured Image

SendMessage2Channel: The Sophisticated Way to Send Teams Webhooks

In the article "Teams Webhooks via PowerShell - Modern Alerting - Part 1", I showed you how to create a state-of-the-art alerting to Teams using PowerShell. So-called webhooks can be sent from PowerShell and received in Teams, so that various alerts or other information appear in the Teams channel:

Screenshot: PowerShell alert in a Teams channel

PowerShell alert as a message card in a Teams channel

To create these so-called message cards, their structure must be described in JSON. Although it is possible to define the necessary information in a hashtable and then convert it to JSON, this procedure is quite complex (especially for beginners) and appears unattractive.

ScriptRunner provides an own function in the ScriptRunner ActionPacks for exactly this use case, which is hidden in the MSTLibrary .

Note: The ScriptRunner ActionsPacks are freely available scripts and functions that solve many of an admin's problems. There is code for all use cases and systems that can be easily obtained and used for free via GitHub - even without the ScriptRunner application. I can highly recommend you to browse through the collection, as there is something for everyone!

The MSTLibrary.ps1 file can be downloaded and used directly from the GitHub web interface. A requirement for using it is the PowerShell module MicrosoftTeams, which you can install directly in PowerShell with the following code line:


Install-Module -Name MicrosoftTeams

In addition to the Teams module, you also need a Teams URL for the webhooks. I showed you how to generate one in the first article of this series.

Now we are ready for a first test run: The MSTLibrary contains the function SendMessage2Channel, which allows us to send message cards with only one line of code.

The function offers the receipt of various parameters, which it then processes. The parameters like title, message, etc. are merged in a hashtable, formatted to JSON and sent to the team URL. Thus, two parameters are mandatory for sending: WebHook and Message.

Another nice feature is the color selection: by default there are the colors "green", "orange" and "red" to choose from, which can be used, for example, to define criticality of an alert - so you could specify as a convention that the color green indicates pure information, the color orange represents a warning and red stands for error. The colors are also recognized by a "ValidateSet" via the auto-completion with the TAB key.

For the graphical representation of the attributes, I have equated the values to the corresponding parameters in the following code example - so you can see at a glance where in the message the attributes are mapped:


$url = ""
$sendArgs = @{
WebhookURL = $url
Message = "Message"
Title = "Title"
MessageColor = Green
ActivityTitle = "ActivityTitle"
ActivitySubtitle = "ActivitySubtitle"   
}
SendMessage2Channel @sendArgs

The SendMessage2Channel function posts an alert message to a Teams channel

Result of the SendMessage2Channel function

Conclusion

As you can see, SendMessage2Channel from ScriptRunner's ActionPack offers a (in my eyes) better option than building a JSON file manually. The code remains much clearer and is also directly understandable for laymen, because the actual functionality behind SendMessage2Channel does not have to be penetrated - the function can be seen as a pure BlackBox, which you feed with data to achieve the desired result.

Related Links

Related posts

10 min read

My top 10 ways to use Teams PowerShell

6 min read

Teams-Webhooks via PowerShell – Modern Alerting (part 3/3)

3 min read

Teams-Webhooks via PowerShell – Modern Alerting (part 2/3)

About the author: