Since I spend a lot more time on Slack, I figured it would be handy to write a PowerShell script that can pass along chat messages to various channels, groups, and users to notify when certain events occur in the lab or elsewhere. Although I did find some examples floating around on the web, they weren’t quite as robust as my needs required. I wanted to be able to pass along parameters such as the channel and message to a function, and then return the results to the parent script that sent the message. The idea behind Post-ToSlack was born! It’s a middle-man function to shoot messages into Slack on behalf of other bits of code. You can download it from GitHub here.
Working with the Slack API is pretty simple due to some great documentation and examples. It requires generating a full-access token for simple scripts (OAuth for other use cases) and then finding a method to use in Slack’s Web API here. Since I wanted to send messages on Slack, the method is chat.postMessage. It requires three arguments:
- token = An authentication token
- channel = The channel, group, or user name
- text = Message text
There’s a pair of optional arguments – username and parse – that also look handy. Username adjusts the name of the bot posting the message, and setting parse to full allows the message text to convert @channel or @username into actual ID values so that it notifies the intended parties. Formatting looked simple enough; just set up a hash table and fire away the contents into the body. A response key named “OK” has a success value of either True or False, which can be passed to the parent script to understand if the call was successful.
Building the Slack cmdlet
I set up four parameters in the PowerShell code. Note that $Channel and $Message are required, while $token and $BotName are not.
My assumption here is that you’re going to pass the $token value in one of two ways: either supply it when you run the PowerShell cmdlet, or save and secure it in a text file named token.txt. If you don’t pass along a value for $token, the script will search for a token.txt file in the running script’s folder. I typically lock down the token file using NTFS. I struggled to find a better way to secure this, so please comment with better ways. 🙂
The $BotName variable is also optional and defaults to PowerShell Bot. Setting the string value in the Param section is a handy way to configure a default without any heavy lifting.
The screen below shows a simple test. Once you load the Post-ToSlack cmdlet (either dot source it or drop it into a module), the command run is:
Post-ToSlack -Channel '#powershell' -Message '@channel Loud noises!' -BotName 'SpongeBob'
And here’s the result!
Calling Post-ToSlack Externally
I don’t want to use the Post-ToSlack function directly. In my lab, the function is called from other code to post alerts and details into Slack. To get your wheels spinning, I’ve written a sample snipet of code which calls the Slack function externally and evaluates the $result variable to determine if the post was successful.
The $result.ok check is validating the key named “OK” to see if it was true (success) or false (failure). This value is derived from the results of the API call made in the Post-ToSlack cmdlet. Here’s the entire set of content returned:
As such, we know that the message was successfully posted. The script has spit out a happy little success blurb to the console and we see the message posted to the Slack channel.