Tuesday 17 February 2015

Submit-Slack awesomeness



I've been playing around with Slack a bit lately.  I don't see us using it internally as a collaboration tool - we're an MS shop that's more likely to go Lync - but I have been using it as a way to get Nagios notifications to my phone.  That works really well and means that I don't have to tie my personal phone to my work email so I can get notifications about important stuff but not random email.

I've also been doing a bit of custom monitoring with various PoSH scripts.  Checking backups have ran, that shadow copies are working, that sort of thing.  That got me thinking, it would be pretty cool to have those PoSH scripts also post to Slack if they come across something I should be aware of, right?  And a great excuse to have a play with RESTful web services through PoSH into the bargain :)

Slack has an integration called Incoming Webhooks that can be used for custom integrations such as this.  Long story short, you can send a JSON payload to Slack which ends up in a channel on your network.  It's very easy to setup too!

I started at Christopher Maneu's post here where he describes how to post to slack using the Invoke-RestMethod cmdlet.  Slack's Incoming Webhooks documentation has everything else you need to know to get this working.

Your finished script will look a little like the one below.  You'll need to use your own Slack network name and token.  I've also included a specified default icon to use with the post if you don't specify one with either the -IconEmoji or -IconURL parameters but that's optional.  If you don't do that, the default icon you configure in Slack for the Incoming Webhooks integration on your network will be used instead.  I've also used parameter sets to make sure that only the -IconEmoji or -IconURL options can be specified but not both.  A very useful feature!

Enjoy :)

No comments:

Post a Comment