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
- Open your Slack Channel
- Click on Add Apps
- Search for Incoming WebHooks
- 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.
- Save your settings
Useful Resources from the official documentation
- App Examples – https://api.slack.com/tutorials/slack-apps-hello-world
- Message Builder – https://api.slack.com/docs/messages/builder
- Instructions for message attachments – https://api.slack.com/docs/message-attachments
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#requires -module ActiveDirectory #Paolo Frigo, https://www.scriptinglibrary.com # The execution of this is scheduled to run every 5 minutes on ServerX # The goal of this script to notify to SLACK Channel: "#GENERAL" channel # every single lockout-event in the local AD domain. # Webhooks Channel $SlackChannelUri = "https://hooks.slack.com/services/ETC..ETC.." $ChannelName = "#general" $BodyTemplate = @" { "channel": "CHANNELNAME", "username": "ActiveDirectory Bot", "text": "*DOMAIN_USERNAME* account is currently locked out. \nTime: DATETIME.", "icon_emoji":":ghost:" } "@ if (Search-ADAccount -LockedOut){ foreach ($user in (Search-ADAccount -LockedOut)){ $body = $BodyTemplate.Replace("DOMAIN_USERNAME","$user").Replace("DATETIME",$(Get-Date)).Replace("CHANNELNAME","$ChannelName") Invoke-RestMethod -uri $SlackChannelUri -Method Post -body $bodytemplate -ContentType 'application/json' } } |
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.
I’m new to Slack, do you know what to do when someone else has already enabled incoming webhooks on a private channel?
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
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
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
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!
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