SlackMessaging.psm1
<#
.Synopsis Sends a message using the Slack API .DESCRIPTION Sends a message using webhooks to Slack, it can send both direct messages or messages to channels You can also customize the name the messages appears to be sent from & the emoji icon. .EXAMPLE Send-SlackMessage -WebhookURL 'https://hooks.slack.com/services/AAA111BBB/CCC2222DDD/a1a1a1a1a1a1a1a1a1a1' -MessageBody 'Some Text \n Line2 of text' This sends a message to slack with 2 lines .EXAMPLE $Webhook = 'https://hooks.slack.com/services/AAA111BBB/CCC2222DDD/a1a1a1a1a1a1a1a1a1a1' Send-SlackMessage -WebhookURL $Webhook -MessageBody 'Hello World!' -channel '#test' -SenderName 'MyAlien' -emoji ':alien:' This uses a WebhookURL stored in a variable to send a single line message to the channel #test from MyAlien with the sender icon being an alien .LINK http://www.f-e-a-r.co.uk #> function Send-SlackMessage { [CmdletBinding()] Param([parameter(mandatory=$true)] [string]$WebhookURL, [parameter(mandatory=$true)] [string]$MessageBody, [string]$SenderName, [string]$channel, [string]$emoji = ':slack:') Write-Verbose "SlackWebhookURI: $WebhookURL" $Msg = "payload={`"text`": `"$MessageBody`" , `"channel`": `"$channel`", `"username`": `"$SenderName`", `"icon_emoji`": `"$emoji`"}" Write-Verbose "JSON Payload: $Msg" Invoke-WebRequest -Uri $WebhookURL -Method Post -Body $Msg Write-Verbose "Command Complete" } |