SendMessage2Channel: the sophisticated way to send team 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:
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.
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:
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:
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.
Philip Lorenz was trained at a large fashion company in Germany and then worked as a specialist for data center and cloud. The focus here was on VMware, Windows Server and Microsoft Azure. His core competence is automating these platforms with PowerShell. Currently, Philip is in a dual study program to become a business information scientist and has built up his own business by providing PowerShell courses, PowerShell coaching and freelancing.