I originally saw Terence Beggs post about sending messages through Microsoft Teams. Not having Teams, I wanted to send the message through Slack instead. This pretty much operates in the same fashion but had to be tweaked in order to work properly. These script(s) are a very minor part of our SCCM environment, but I really love getting notified when either in-place upgrades are complete, or computers are imaged.
Slack Application Setup
The first step is to head over to the Slack API site and create an app. Name your app as you please.
After you create your application, you’ll need to create an incoming webhook. You’ll assign it to a channel in Slack, so I recommend creating a new one for notification. That way other people can mute the channel if it gets too noisy. Make note of your webhook URL as we’ll need this later in the PowerShell script.
SCCM/Task Sequence Setup
Now onto SCCM. Use the Get-TSDuration.ps1, and slack_sccm_message.ps1 located here, and modify the main slack_sccm_message to include your webhook URL in the URI variable. Create a package (no program) for the folder that includes both scripts. Distribute the package to the applicable distribution points in your environment. Get-TSDuration.ps1 will set task sequence variables, based on a parameter, for the start and end of a task sequence. It’ll later be used in a time span to get the total time for the task sequence.
In your task sequence, create a step to Run a PowerShell Script. Select the package you just created, use the script name and Specify the Start parameter.
Do the same thing for the end of your task sequence, but obviously add the step to the end of the task sequence, and replace the Start parameter with End. If you’re doing any sort of error handling for failures, you might need a second End parameter for the error handling portion. This will also hold true for the slack message script if you want to send messages for Task Sequence failures as well.
Add the slack_sccm_message script right after your Get-TSDuration ending scripts. I’m using a PassFail parameter to format the message differently based on if the task sequence succeeds or fails.
What you should wind up with is a nicely formatted slack message like this:
You’ll notice that there are links to images for passing and failing task sequences, task sequence variables, and static text. This can obviously be formatted to your liking. Let me know what other changes you’ve come up with, or if you’re adding something additional that you think is helpful to know.
Awesome work man!
Something I’ve noticed is that the fail will only work if it’s progressed passed the Apply operating system step and rebooted, as PS is not available in your boot image – do you know which powershell component would be required in the boot image?
We have both WinPE-DismCmdlets and WinPE-PowerShell added. We do use Dism for drivers and language packs
I’m trying to get this to slack call the technician. In my OSD we have a drop down list to select the tech doing the deploy, this works and returns the techs name in to Slack, but isn’t picking up the @tech_name as a slack call, it’s just plaintext @tech_name.
Sorry for the late reply. It looks like the markdown needs to be in this format . I haven’t tested this yet, but it should be easy to just send a test message real quick
Sorry, the code should be
I had to go here to get the list of User IDs https://api.slack.com/methods/users.list/test
This works great! I have one question though, the duration doesn’t seem to work. I didn’t modify the script outside of entering my URI. What should I check? The end time shows up but the duration just shows **.
If you’re inside a PXE environment, I’ve found that the computer doesn’t always have access to the time and date. I replaced “get-date” with an API call to worldclockapi. Try using this for the start and end time variables instead “(Invoke-RestMethod -Uri ‘http://worldclockapi.com/api/json/utc/now’ -Method GET).currentDateTime”
[…] Sending Messages in OSD to Slack by John Kuntz […]