Automate Slack posts with Invoke-RestMethod and Webhooks

By | March 31, 2018

I love free learning resources and I recently discovered Packt publishing give away a free full ebook everyday from their massive library that covers everything software development and system administration to databases and agile/lean.

The trouble is that with everything I’ve got going on these days I totally forgot check it one day and missed a free book that really wanted.

Being that I like to automate the boring stuff, coupled with scheduling it to run daily, I figured I’d never miss another day.

So I’ve looked to send myself and colleagues a reminder everyday on one of my daily tools for communications, Slack. For this I’m going to use the Incoming WebHooks app to take an incoming post request using PowerShell’s Invoke-RestMethod.

At my work we currently communicate with Slack most of the time. It’s a great tool that puts all team communications is one place. It also gets rid of the pain of trying to keep track of email threads a mile deep. Even though the Slack communications happen in the one place it can also be broken up and segmented by creating “channels” for different topics.

I’ve created a #freelearning channel where I’m going to post the reminder.

If you don’t have the app installed already just do a search in the Slack “App Directory”, as shown below. And install the “Incoming WebHooks” app.

Once installed click on the app to configure the settings. Either specify a channel or create a new one, this where the incoming post will be published. A click “Add Incoming WebHook integration”.

This will then provide you with the WebHook URL and some information on sending messages using a JSON formatted “payload”.

You can see that the “payload” is pretty much a hash table with a key/value pair. So this gives us a couple of ways to present the information as JSON. We could write is as a hash table and and use the ConvertTo-Json Cmdlet to convert it.

Or use a here string to write it out in JSON.

Now to send the payload with the WebHook we can use PowerShell’s Invoke-RestMethod with the URL that was created when we setup the app within Slack and using the POST method.


Here String

And below shows the link that gets output to the Slack channel, where you can click the lick and go download the your free ebook. I’ve added this to a Jenkins job that is just scheduled to run a PowerShell script with the above command each morning.

Leave a Reply

Your email address will not be published. Required fields are marked *