How to Send Messages to Slack with PowerShell

Collaboration software tools are present in some shape or form in every workplace and if properly implemented and organised will give team members the opportunity of working together not only in an efficient way, but getting the job done while having fun!

Integration with these tools is not just desirable, it’s sometimes required. The nature of instant messaging communication in each workplace may vary depending on the content and information can be volatile and informal or more persistent and organised.

Using Powershell to integrate with Teams or Slack

One of the most popular articles here on scripting library was using PowerShell to send messages to Microsoft Teams. The scenario for the integration was scheduling a script (every 5 minutes) written in PowerShell that checked in Active Directory for any Locked Out user and if that was the case then sending the notification on the MS Teams Channel to inform the Support or Operations Teams involved within the organisation.

I strongly recommend you to read this article because in this one I will give for granted that you’re already familiar with SAAS applications and WebHooks.

How to trigger incoming webhooks in Microsoft Teams with Powershell

Slack was the next popular tool to cover

It may seem obvious, but I think that is sometimes required to show on different tools how can we reach the same goal, and in just a few steps is possible to have the same integration completed.

 

How to configure Slack to receive messages via WebHooks

  1. Open your Slack Channel
  2. Click on Add Apps
  3. Search for Incoming WebHooks
  4. Complete the integration Settings choosing the Channel where you want to post and copy the Webhook URL to paste it later in the PowerShell script below.
  5. Save your settings

Useful Resources from the official documentation

Powershell Script

I’ve revisited the script created in the previous article and simply adapted for Slack, the only meaningful change are on the posted JSON and the different WebHook URL as you probably expect:

This will be the message received in SLACK

I hope you’ve found this article interesting and if you have any questions or feedback feel free to contact me, as usual, my code is available on my GitHub repository.

6 Replies to “How to Send Messages to Slack with PowerShell”

    1. Hi Scott, Thanks for your comment. If you’re familiar with MS Teams the concept is the same. If there is already a custom incoming webhook you have at least 2 choices that I can think of.
      They both work for you, I guess. The options are Re-Use that one or add a new one, personally, my favourite choice is to configure multiple ones.
      The benefit is to have different settings for each webhook, such different icon for instance and I can for that is that I can disable one if for some reason it starts to flood the chat or the security token is compromised. Regards

  1. Hi,

    Great article and thanks for sharing. I have this running as a scheduled task.

    All work great, apart it displays the whole LDAP namespace instead of the user. Any help would be greatly appreciated.
    Thanks,
    Toams

    1. Hi Tom,
      Replace this
      $BodyTemplate.Replace("DOMAIN_USERNAME","$user")
      with
      $BodyTemplate.Replace("DOMAIN_USERNAME","$($user.samaccountname)")
      Search-Adaccount returns an ADUser object, you’re interest in the name (name) or username(samaccountame) so you can get that property.

      Thanks for your comment and I’m happy this code helped you in your daily job! Cheers

  2. Hi! This worked wonders, just to tell you that there is a litle typo that can get confusion to somes,

    Invoke-RestMethod -uri $SlackChannelUri -Method Post -body $body -ContentType ‘application/json’

    But you var its called: $BodyTemplate
    Nothing serious!.
    Amazing job btw, just what i was looking!

    1. Hi Alexis,
      Thanks for your comment and feedback, I’ve fixed the variable name. Happy that you have found this article and script useful!
      Regards

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.