• Stars
    star
    266
  • Rank 148,779 (Top 4 %)
  • Language
    PowerShell
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PowerShell module for simple Slack integration

Build status

PSSlack

This is a quick and dirty module to interact with the Slack API.

Pull requests and other contributions would be welcome!

Instructions

# One time setup
    # Download the repository
    # Unblock the zip
    # Extract the PSSlack folder to a module path (e.g. $env:USERPROFILE\Documents\WindowsPowerShell\Modules\)
# Or, with PowerShell 5 or later or PowerShellGet:
    Install-Module PSSlack

# Import the module.
    Import-Module PSSlack    #Alternatively, Import-Module \\Path\To\PSSlack

# Get commands in the module
    Get-Command -Module PSSlack

# Get help
    Get-Help Send-SlackMessage -Full
    Get-Help about_PSSlack

Prerequisites

Examples

Send a Simple Slack Message

# This example shows a crudely crafted message without any attachments,
# using parameters from Send-SlackMessage to construct the message.

#Previously set up Uri from https://<YOUR TEAM>.slack.com/apps/A0F7XDUAZ
$Uri = "Some incoming webhook uri from Slack"

Send-SlackMessage -Uri $Uri `
                  -Channel '@wframe' `
                  -Parse full `
                  -Text 'Hello @wframe, join me in #devnull!'

# Send a message to @wframe (not a channel), parsing the text to linkify usernames and channels

      Simple Send-SlackMessage

Search for a Slack Message

# Search for a message containing PowerShell, sorting results by timestamp

Find-SlackMessage -Token $Token `
                  -Query 'PowerShell' `
                  -SortBy timestamp

      Find Message

# Search for a message containing PowerShell
# Results are sorted by best match by default
# Notice the extra properties and previous/next messages

Find-SlackMessage -Token $Token `
                  -Query 'PowerShell' |
    Select-Object -Property *

      Find Message Select All

You could use this simply to search Slack from the CLI, or in an automated solution that might avoid posting if certain content is already found in Slack.

Send a Richer Slack Message

# This is a simple example illustrating some common options
# when constructing a message attachment
# giving you a richer message

$Token = 'A token. maybe from https://api.slack.com/docs/oauth-test-tokens'

New-SlackMessageAttachment -Color $([System.Drawing.Color]::red) `
                           -Title 'The System Is Down' `
                           -TitleLink https://www.youtube.com/watch?v=TmpRs7xN06Q `
                           -Text 'Please Do The Needful' `
                           -Pretext 'Everything is broken' `
                           -AuthorName 'SCOM Bot' `
                           -AuthorIcon 'http://ramblingcookiemonster.github.io/images/tools/wrench.png' `
                           -Fallback 'Your client is bad' |
    New-SlackMessage -Channel '@wframe' `
                     -IconEmoji :bomb: |
    Send-SlackMessage -Token $Token

      Rich messages

Notice that the title is clickable. You might link to...

  • The alert in question
  • A logging solution query
  • A dashboard
  • Some other contextual link
  • Strongbad

Send Multiple Slack Attachments

# This example demonstrates that you can chain new attachments
# together to form a multi-attachment message

$Token = 'A token. maybe from https://api.slack.com/docs/oauth-test-tokens'

New-SlackMessageAttachment -Color $_PSSlackColorMap.red `
                           -Title 'The System Is Down' `
                           -TitleLink https://www.youtube.com/watch?v=TmpRs7xN06Q `
                           -Text 'Everybody panic!' `
                           -Pretext 'Everything is broken' `
                           -Fallback 'Your client is bad' |
    New-SlackMessageAttachment -Color $([System.Drawing.Color]::Orange) `
                               -Title 'The Other System Is Down' `
                               -TitleLink https://www.youtube.com/watch?v=TmpRs7xN06Q `
                               -Text 'Please Do The Needful' `
                               -Fallback 'Your client is bad' |
    New-SlackMessage -Channel '@wframe' `
                     -IconEmoji :bomb: `
                     -AsUser `
                     -Username 'SCOM Bot' |
    Send-SlackMessage -Token $Token

      Multiple Attachments

Notice that we can chain multiple New-SlackMessageAttachments together.

Send a Table of Key Value Pairs

# This example illustrates a pattern where you might
# want to send output from a script; you might
# include errors, successful items, or other output

# Pretend we're in a script, and caught an exception of some sort
$Fail = [pscustomobject]@{
    samaccountname = 'bob'
    operation = 'Remove privileges'
    status = "An error message"
    timestamp = (Get-Date).ToString()
}

# Create an array from the properties in our fail object
$Fields = @()
foreach($Prop in $Fail.psobject.Properties.Name)
{
    $Fields += @{
        title = $Prop
        value = $Fail.$Prop
        short = $true
    }
}

$Token = 'A token. maybe from https://api.slack.com/docs/oauth-test-tokens'

# Construct and send the message!
New-SlackMessageAttachment -Color $([System.Drawing.Color]::Orange) `
                           -Title 'Failed to process account' `
                           -Fields $Fields `
                           -Fallback 'Your client is bad' |
    New-SlackMessage -Channel 'devnull' |
    Send-SlackMessage -Uri $uri

# We build up a pretend error object, and send each property to a 'Fields' array
# Creates an attachment with the fields from our error
# Creates a message fromthat attachment and sents it with a uri

      Fields

Store and Retrieve Configs

To save time and typing, you can save a token or uri to a config file (protected via DPAPI) and a module variable.

This is used as the default for commands, and is reloaded if you open a new PowerShell session.

# Save a Uri and Token.
# If both are specified, token takes precedence.
Set-PSSlackConfig -Uri 'SomeSlackUri' -Token 'SomeSlackToken'

# Read the current cofig
Get-PSSlackConfig

Notes

Currently evaluating .NET Core / Cross-platform functionality. The following will not work initially:

  • Serialization of URIs and tokens via Set-PSSlackConfig. Set these values per-session if needed
  • [System.Drawing.Color]::SomeColor shortcut. Use the provided $_PSSlackColorMap hash to simplify this. E.g. $_PSSlackColorMap.red

There are a good number of Slack functions out there, including jgigler's PowerShell.Slack and Steven Murawski's Slack. We borrowed some ideas and code from these - thank you!

If you want to go beyond interacting with the Slack API, you might consider using a bot

More Repositories

1

PowerShell

Various PowerShell functions and scripts
PowerShell
934
star
2

Invoke-Parallel

Speed up PowerShell with simplified multithreading
PowerShell
374
star
3

PSDeploy

Simple PowerShell based deployments
PowerShell
343
star
4

PSSQLite

PowerShell module to query SQLite databases
PowerShell
287
star
5

PSDepend

PowerShell Dependency Handler
PowerShell
273
star
6

PSExcel

A simple Excel PowerShell module
PowerShell
243
star
7

BuildHelpers

Helper functions for PowerShell CI/CD scenarios
PowerShell
208
star
8

SecretServer

Secret Server PowerShell Module
PowerShell
87
star
9

PSStackExchange

PowerShell module to query Stack Exchange API
PowerShell
80
star
10

PSRabbitMq

PowerShell module to send and receive messages from a RabbitMq server
PowerShell
45
star
11

PSHTMLTable

PowerShell module to spice up ad hoc notifications and reports
PowerShell
37
star
12

Git-Presentation

Presentation materials for Git and GitHub TechSession
33
star
13

WritingModules

Material accompanying PowerShell + DevOps Summit session
PowerShell
31
star
14

PSDiskPart

DiskPart PowerShell Module
PowerShell
31
star
15

Infoblox

Infoblox PowerShell Module
PowerShell
30
star
16

PSNeo4j

Simple Neo4j PowerShell Wrapper
PowerShell
29
star
17

ADGrouper

Define dynamic AD security group membership via yaml
PowerShell
18
star
18

Dots

A janky, neo4j based CMDB glued together with PowerShell
PowerShell
15
star
19

Citrix.NetScaler

PowerShell module for working with Citrix NetScaler REST API
PowerShell
15
star
20

PSRT

PowerShell wrapper for Request Tracker
PowerShell
10
star
21

TireFire

A janky PowerShell module to simplify managing notes and their metadata
PowerShell
7
star
22

PSStash

Atlassian Stash PowerShell Module
PowerShell
7
star
23

PSPagerDuty

Simple PowerShell PagerDuty module
PowerShell
7
star
24

PSLDAPQueryLogging

PowerShell module to simplify configuring AD LDAP diagnostic logging
PowerShell
7
star
25

PSPuppetDB

Simple module for querying the PuppetDB API
PowerShell
6
star
26

Wait-Path

Wait for a path to exist
PowerShell
5
star
27

CommunityLightningDemos2017

Proposals, and eventually demo material for Community Lightning Demos
PowerShell
5
star
28

PSSensu

Simple PowerShell module for working with the Sensu Go API
PowerShell
3
star
29

SessionMaterials

Materials or links to materials from sessions I've participated in
PowerShell
3
star
30

AppVeyor-DSC-Test

POC to test DSC configurations on a fresh VM from AppVeyor
PowerShell
2
star
31

AppVReporting

App-V Reporting PowerShell Module
PowerShell
2
star
32

RamblingCookieMonster.github.io

CSS
2
star
33

lisa-kitchen-demo

Example used for Test-Kitchen demo
PowerShell
2
star
34

wip

Stuff that doesn't have a home yet
PowerShell
1
star
35

zAppVeyor-Explore

PowerShell
1
star