5 min read
Tip #3: Utilizing external config data in PowerShell (1/4)
Tobias & Aleksandar's tip #3:
The two very well-known PowerShell experts have teamed up to share their best and most...
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.
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
Result of the SendMessage2Channel function
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.
Sep 4, 2024 by Dr. Tobias Weltner and Aleksandar Nikolić
The two very well-known PowerShell experts have teamed up to share their best and most...
Sep 4, 2024 by Aleksandar Nikolić and Dr. Tobias Weltner
The two very well-known PowerShell experts have teamed up to share their best and most...
Sep 4, 2024 by Dr. Tobias Weltner and Aleksandar Nikolić
The two very well-known PowerShell experts have teamed up to share their best and most...
Philip Lorenz works as a DevOps and cloud engineer. In addition to this activity, he holds training courses and creates learning content on the topics of PowerShell, automation and cloud computing.