Last active
December 22, 2015 13:41
-
-
Save jhochwald/d8b6ba454fdc74e308b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
{ | |
"info": { | |
"Statement": "Code is poetry", | |
"Author": "Joerg Hochwald", | |
"Contact": "[email protected]", | |
"Link": "http://hochwald.net", | |
"Support": "https://github.com/jhochwald/MyPowerShellStuff/issues" | |
}, | |
"Copyright": "(c) 2012-2015 by Joerg Hochwald. All rights reserved." | |
} | |
Redistribution and use in source and binary forms, with or without modification, | |
are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this list of | |
conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation and/or | |
other materials provided with the distribution. | |
3. Neither the name of the copyright holder nor the names of its contributors may | |
be used to endorse or promote products derived from this software without | |
specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | |
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
POSSIBILITY OF SUCH DAMAGE. | |
By using the Software, you agree to the License, Terms and Conditions above! | |
#> | |
function Send-SlackChat { | |
<# | |
.SYNOPSIS | |
Sends a chat message to a Slack organization | |
.DESCRIPTION | |
The Post-ToSlack cmdlet is used to send a chat message to a Slack channel, group, or person. | |
Slack requires a token to authenticate to an org. | |
.PARAMETER Channel | |
Slack Channel to post to | |
.PARAMETER Message | |
Chat message to post | |
.PARAMETER token | |
Slack API token | |
.PARAMETER BotName | |
Optional name for the bot | |
.EXAMPLE | |
PS C:\> Send-SlackChat -channel '#general' -message 'Hello everyone!' -botname 'The Borg' -token '1234567890' | |
This will send a message to the "#General" channel using a specific token 1234567890, and the bot's name will be "The Borg". | |
.EXAMPLE | |
PS C:\> Send-SlackChat -channel '#general' -message 'Hello everyone!' -token '1234567890' | |
This will send a message to the "#General" channel using a specific token 1234567890, and the bot's name will be default ("Build Bot"). | |
.NOTES | |
Based on an idea of @ChrisWahl / Please note the Name change and the removal of some functions | |
.LINK | |
Validate or update your Slack tokens: https://api.slack.com/tokens | |
Create a Slack token: https://api.slack.com/web | |
More information on Bot Users: https://api.slack.com/bot-users | |
#> | |
[CmdletBinding(ConfirmImpact = 'None', | |
SupportsShouldProcess = $true)] | |
param | |
( | |
[Parameter(Mandatory = $true, | |
Position = 0, | |
HelpMessage = 'Slack Channel to post to')] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
$Channel, | |
[Parameter(Mandatory = $true, | |
Position = 1, | |
HelpMessage = 'Chat message to post')] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
$Message, | |
[Parameter(Mandatory = $false, | |
Position = 2, | |
HelpMessage = 'Slack API token')] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
$token, | |
[Parameter(Mandatory = $false, | |
Position = 3, | |
HelpMessage = 'Optional name for the bot')] | |
[Alias('Name')] | |
[string] | |
$BotName = 'Build Bot' | |
) | |
BEGIN { | |
# Cleanup all variables... | |
Remove-Variable -Name "uri" -Force -Confirm:$false -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue | |
Remove-Variable -Name "body" -Force -Confirm:$false -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue | |
} | |
PROCESS { | |
Set-Variable -Name "uri" -Value $("https://slack.com/api/chat.postMessage") | |
# Build the body as per https://api.slack.com/methods/chat.postMessage | |
Set-Variable -Name "body" -Value $(@{ | |
token = $token | |
channel = $Channel | |
text = $Message | |
username = $BotName | |
parse = 'full' | |
}) | |
# Call the API | |
try { | |
Set-Variable -Name "post" -Value $(Invoke-RestMethod -Uri $uri -Body $body -UserAgent "Mozilla/5.0 (Windows NT; Windows NT 6.1; en-US) NET-Experts WindowsPowerShell Service $CoreVersion" -ErrorAction Stop -WarningAction SilentlyContinue) | |
} catch { | |
# Whoopsie! That should not happen... | |
Write-Warning -message "Could not send notification to your Slack $Channel" | |
} | |
} | |
END { | |
# Cleanup all variables... | |
Remove-Variable -Name "uri" -Force -Confirm:$false -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue | |
Remove-Variable -Name "body" -Force -Confirm:$false -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue | |
# Do a garbage collection | |
if ((Get-Command run-gc -errorAction SilentlyContinue)) { | |
run-gc | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment