Create Slack messages using PowerShell

If you’re a Slack user like myself, you can use PowerShell to send messages to your Slack channel. This is very useful for scripts that run during the day on which you want to receive information. This is done by using the Invoke-Webrequest method and in this blog post, I will show you how you can use it in your scripts.

Configuring the Slack application

You can send messages using a Token, to be able to do this you need to register an application. You can do this by following the following steps:

  • Enter an App Name, PowerShellSlack in this example, select the Slack workspace the app should be a member of, and select Create App
  • Select Incoming Webhooks
  • Push the slider to On and select Add New Webhook to Workspace
  • Select the channel that you want to post messages in, for this example I created a logs channel
  • Select Allow
  • You will return to the app and you can copy the Webhook URL that you will need in your scripts by using the Copy button, paste the URL somewhere safe ๐Ÿ™‚
  • Now you need to set permissions for the OAth token by scrolling down the page and selecting OAuth & Permissions
  • In the Scopes section you need to add chat:write, chat:write:public and files:write. The incoming webhook should already be present because we added that earlier. Choose to Add an Oath Scope and type/browse/select these three permissions.
  • After adding them, a notification banner will pop up at the top of your browser, select Reinstall your app so that the permissions are added.
  • Select the channel again and choose Allow
  • Copy the Bot User OAuth Token using the Copy button. (Keep this information safe too ๐Ÿ™‚ )

Send your first message to the Slack channel

Now that the registration is done, you can start posting your first message. The most basic way to send a message is by using this : (I removed a part of the URL and replaced it with xxx)

$url = 'https://slack.com/api/chat.postMessage'
$message = 'Hello World!'
$token = 'xoxb-xxx'
$channel = 'logs'
$body = @{token = $token; channel = $channel; text = $message; pretty = 1 }
Invoke-WebRequest -Uri $url -Method POST -Body $body

If all goes well, you should see this output with a HTTP status code 200:

In the Slack you should see this:

Send a message to the Slack channel containing an attachment

You can also send, for example, a log file together with the message to a Slack channel. First, you will have to invite PowerShellSlack-bot to the channel, you can do this by mentioning the bot using the @-sign and selecting the app:

After pressing Enter, you select Add to Channel

You will get a notification in the channel if it succeeds:

The next step is to copy the Channel ID, you can find this in the properties of the Channel:

The Channel ID is down left, you can click the icon next to it to copy the ID.

Now you can use the script below to send a message with the contents of a log file to a Slack channel

$url = "https://slack.com/api/files.upload"
$initialcomment = "Sync done, see log output below"
$token = "xoxb-3xxx"
$channels = "C03ETRXS0AC"
$content = get-content d:\scripts\software_inventory\log.txt | Out-String
$title = "log.txt"
$body = @{
    token           = $token; 
    initial_comment = $initialcomment; 
    channels        = $channels; 
    content         = $content; 
    title           = $title ;
    pretty          = 1
}
Invoke-WebRequest -Uri $url -Method POST -Body $body

After running the above script, you should see this output:

And the message will look like this:

And that’s it ๐Ÿ™‚ You can find more information about the possibilities of the Slack API methods here: https://api.slack.com/methods Happy PowerShell Slacking!

Leave a Reply

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