Skip to content

Instantly share code, notes, and snippets.

@rossnz
Last active September 25, 2017 22:26
Show Gist options
  • Save rossnz/1b0163d6126c0b041435556f6ca7842b to your computer and use it in GitHub Desktop.
Save rossnz/1b0163d6126c0b041435556f6ca7842b to your computer and use it in GitHub Desktop.
function Post-ToSlack
{
Param(
[Parameter(Mandatory = $true,Position = 0,HelpMessage = 'Chat message to send')]
[ValidateNotNullorEmpty()]
[String]$Message,
[Parameter(Mandatory = $true,Position = 1,HelpMessage = 'Slack webhook URL')]
[ValidateNotNullorEmpty()]
[String]$uri,
[Parameter(Mandatory = $false,Position = 2,HelpMessage = 'Slack channel')]
[ValidateNotNullorEmpty()]
[String]$channel = '#general',
[Parameter(Mandatory = $false,Position = 3,HelpMessage = 'Slack username')]
[ValidateNotNullorEmpty()]
[String]$username = 'PowerShell script',
[Parameter(Mandatory = $false,Position = 4,HelpMessage = 'Icon emoji')]
[ValidateNotNullorEmpty()]
[String]$icon_emoji = ":bomb:"
)
Process {
$payload = [PSCustomObject] @{
username = $username
channel = $channel
icon_emoji = $icon_emoji
text = $message
}
$body = $payload | ConvertTo-Json
# Call the API
try
{
Invoke-RestMethod -Uri $uri -Method POST -ContentType "application/json" -Body $body
}
catch
{
throw 'Unable to call the API'
}
} # End of process
} # End of function
# See https://api.slack.com/apps
# and https://my.slack.com/services/new/incoming-webhook/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment