How to Send Slack Messages with PowerShell

How to Send Slack Messages with PowerShell

One of the best ways to stay in contact with your remote team is a team collaboration service like Slack.

Slack has become one of the most popular platforms, and it’s easy to see why. The easy-to-use platform has an intuitive interface and a robust API with lots of apps that tie into many popular services such as Dropbox, Github, and Trello.

If your favorite service doesn’t provide a built-in Slack integration, that’s not a problem. Using Slack’s API, you can build just about anything you’d like. In this article, you’re going to learn how to send Slack notifications using PowerShell. Why PowerShell? Because you now have the power to send a Slack notification when a monitor fails, when a task in your DevOps release pipeline fails or a simple direct message to yourself if a personal script triggers.

By using Slack’s API and a PowerShell module, you can integrate any kind of message you’d like to send to Slack via a simple PowerShell command. Here are some tips on how to establish a Slack channel as a notification hub for your team.

 

Creating a Slack App

First off, create a Slack app by navigating to the Create a Slack app page providing the app name and workspace.

PowerShell will connect to Slack’s API so you need to create an Incoming Webhook.

On the Incoming Webhooks screen, activate the option then click on Add New Webhook to Workspace as shown below.

Select the channel you’d like to associate the webhook with and click on Allow.

You should then be brought back to the Incoming Webhooks page where you’ll see a webhook URL. Be sure to click Copy and paste this somewhere for safe-keeping.

After you have the webhook URL, you’re ready to drop into PowerShell.

Using the PSSlack Module

Next, open up PowerShell and download Warren Frame’s PSSlack module. It’s possible to establish a notification hub without the help of this module, but using it will save you a lot of time when it comes to figuring out how to interact with Slack’s API via PowerShell.

When you’re ready to send a message to your Slack channel with PowerShell, use the Send-SlackMessage function in the PSSlack module. While there are a couple of different ways to send a slack message, the easiest method may be to simply use the webhook URI. After you ensure that you have the PSSlack module loaded into your session, you can use the Send-SlackMessage function by specifying the webhook URI as well as the message text:

Send-SlackMessage -Uri <webhook URI> -Text 'testing'

This will send the message to the channel that you have configured in your incoming webhook configuration on Slack.

As-is, you must provide that long webhook URL every time you need to send a notification. You can eliminate that a few different ways; one way is to create a function with a default parameter.

Below you can see an example of a function called Send-Notification. Using this function, you can simply run Send-Notification -Message '<some message>' to send a notification to the channel specified via the incoming webhook. No more memorizing the webhook URL!

Function Send-Notification {
    Param(
        [Parameter(Mandatory)]
        [string]$Message,
 
        [Parameter()]
        [string]$SlackWebhookURI = '<webhook URI>'
    )
 
    Send-SlackMessage -Uri $SlackWebhookURI -Text $Message
}

Conclusion

We just touched the surface on what’s possible using Slack’s API and PowerShell. I highly encourage you to look deeper into the Slack API and see what you can come up with. Chances are you can use the PSSlack PowerShell module or even build your own to automate any kind of notifications you wish.

 

Related Posts


Comments
Comments are disabled in preview mode.
Loading animation