Skip to content

Instantly share code, notes, and snippets.

@jhochwald
Created January 29, 2016 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhochwald/f177b5617abd15a34318 to your computer and use it in GitHub Desktop.
Save jhochwald/f177b5617abd15a34318 to your computer and use it in GitHub Desktop.
A while ago I published some other functions for Chat and messaging, here is one for Pushover
#region License
<#
{
"info": {
"Statement": "Code is poetry",
"Author": "Joerg Hochwald",
"Contact": "joerg.hochwald@outlook.com",
"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!
#>
#endregion License
function Send-Pushover {
<#
.SYNOPSIS
Sends a push message via Pushover
.DESCRIPTION
We established a lot of automated messaging and push services, Pushover was missing!
We do not use Pushover that much, but sometimes it is just nice to have the function ready...
.EXAMPLE
PS C:\> Send-Pushover -User "USERTOKEN" -token "APPTOKEN" -Message "Test"
Send the message "Test" to all your devices. The App Name is displayed a title of the push
.EXAMPLE
PS C:\> Send-Pushover -User "USERTOKEN" -token "APPTOKEN" -Message "Test" -device "Josh-iPadPro"
Send the message "Test" to the device with the name "Josh-iPadPro". The App Name is displayed a title of the push
.EXAMPLE
PS C:\> Send-Pushover -User "USERTOKEN" -token "APPTOKEN" -Message "Test" -title "Hello!" -sound "cosmic"
Send the message "Test" to all your devices. It will have the Title "Hello!" and use the notification sound "cosmic"
.EXAMPLE
PS C:\> Send-Pushover -User "USERTOKEN" -token "APPTOKEN" -Message "Nice URL for you" -title "Hello!" -url "http://hochwald.net" -url_title "My Site"
Send the message "Nice URL for you" with the title "Hello!" to all your devices. The Push conains a link to "http://hochwald.net" with the URL title "My Site"
.PARAMETER User
the user/group key (not e-mail address) of your user (or you), viewable when logged into our Pushover dashboard
.PARAMETER Message
Your message, can be HTML like formated
.PARAMETER token
Your Pushover application API token
.PARAMETER device
Your device name to send the message directly to that device, rather than all of the devices (multiple devices may be separated by a comma)
.PARAMETER title
Your message title, otherwise your app name is used
.PARAMETER url
A supplementary URL to show with your message
.PARAMETER url_title
A title for your supplementary URL, otherwise just the URL is shown
.PARAMETER priority
The Push priority (-2 to +2)
.PARAMETER sound
The name of one of the sounds supported by device clients to override the user's default sound choice
.NOTES
Based on our Send-SlackChat function
.LINK
Info: https://pushover.net
.LINK
API: https://pushover.net/api
.LINK
Send-SlackChat
#>
[CmdletBinding(ConfirmImpact = 'None',
SupportsShouldProcess = $true)]
param
(
[Parameter(Mandatory = $true,
Position = 0,
HelpMessage = 'the user/group key (not e-mail address) of your user (or you), viewable when logged into our Pushover dashboard')]
[ValidateNotNullOrEmpty()]
[System.String]$User,
[Parameter(Mandatory = $true,
Position = 1,
HelpMessage = 'Your message, can be HTML like formated')]
[ValidateNotNullOrEmpty()]
[System.String]$Message,
[Parameter(Mandatory = $false,
Position = 2,
HelpMessage = 'Your Pushover application API token')]
[ValidateNotNullOrEmpty()]
[System.String]$token,
[Parameter(HelpMessage = 'Your device name to send the message directly to that device, rather than all of the devices (multiple devices may be separated by a comma)')]
$device,
[Parameter(HelpMessage = 'Your message title, otherwise your app name is used')]
$title,
[Parameter(HelpMessage = 'A supplementary URL to show with your message')]
$url,
[Parameter(HelpMessage = 'A title for your supplementary URL, otherwise just the URL is shown')]
$url_title,
[Parameter(HelpMessage = 'The Push priority (-2 to +2)')]
[ValidateSet('-2', '-1', '0', '1', '2')]
$priority = "0",
[Parameter(HelpMessage = 'The name of one of the sounds supported by device clients to override the user''s default sound choice')]
[ValidateSet('pushover', 'bike', 'bugle', 'cashregister', 'classical', 'cosmic', 'falling', 'gamelan', 'incoming', 'intermission', 'magic', 'mechanical', 'pianobar', 'siren', 'spacealarm', 'tugboat', 'alien', 'climb', 'persistent', 'echo', 'updown', 'none')]
$sound = "pushover"
)
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
Remove-Variable -Name "myBody" -Force -Confirm:$false -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue
Remove-Variable -Name "myMethod" -Force -Confirm:$false -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue
}
PROCESS {
Set-Variable -Name "uri" -Value $("https://api.pushover.net/1/messages.json")
# Build the body as per https://pushover.net/faq#library
# We convert this to JSON then...
Set-Variable -Name "body" -Value $(@{
token = $token
user = $User
message = $Message
})
# Sent a push to a special Device? Could be a list seperated by comma
if ($device) {
$TmpBody = @{ device = $device }
$body = $body + $TmpBody
$TmpBody = $null
}
# Want a Title for this Push?
if ($title) {
$TmpBody = @{ title = $title }
$body = $body + $TmpBody
$TmpBody = $null
}
# Attach a URL to the push?
if ($url) {
# Encode the URL if possible
if ((Get-Command ConvertTo-UrlEncoded -errorAction SilentlyContinue)) {
try {
$url = (ConvertTo-UrlEncoded $url)
} catch {
# Argh! Use a unencoded URL
$UrlEncoded = ($url)
}
} else {
# Use a unencoded URL
$UrlEncoded = ($url)
}
$TmpBody = @{ url = $UrlEncoded }
$body = $body + $TmpBody
$TmpBody = $null
}
# Give the URL a nice title. Just URLs suck!
if ($url_title) {
$TmpBody = @{ url_title = $url_title }
$body = $body + $TmpBody
$TmpBody = $null
}
# Set a Priotity for this push
if ($priority) {
$TmpBody = @{ priority = $priority }
$body = $body + $TmpBody
$TmpBody = $null
}
# Special Sound?
if ($sound) {
$TmpBody = @{ sound = $sound }
$body = $body + $TmpBody
$TmpBody = $null
}
# Convert the Body Variable to JSON Check if the Server understands Compression,
# could reduce bandwidth Be careful with the Depth Parameter, bigger values means less performance
Set-Variable -Name "myBody" -Value $(ConvertTo-Json $body -Depth 2 -Compress:$false)
# Method to use for the RESTful Call
Set-Variable -Name "myMethod" -Value $("POST" -as ([System.String] -as [type]))
# Use the API via RESTful call
try {
(Invoke-RestMethod -Uri $uri -Method $myMethod -Body $body -UserAgent "Mozilla/5.0 (Windows NT; Windows NT 6.1; en-US) NET-Experts WindowsPowerShell Service $CoreVersion" -ErrorAction Stop -WarningAction SilentlyContinue)
} catch [System.Exception] {
<#
Argh!
That was an Exception...
#>
# Convert the Exception to a usable String
$exceptionMessage = $_.Exception.ToString()
# Show a warning (No error) with the Infos...
Write-Warning -message "Caught exception $exceptionMessage during execution of URI $uri"
Write-Warning -message "Could not send notification to your Slack $User"
Remove-Variable -Name "exceptionMessage" -Force -Confirm:$false -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue
} catch {
# Whoopsie!
# That should not happen...
Write-Warning -message "Could not send notification to your Slack $User"
} finally {
# 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
Remove-Variable -Name "myBody" -Force -Confirm:$false -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue
Remove-Variable -Name "myMethod" -Force -Confirm:$false -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue
}
}
END {
# Do a garbage collection
if ((Get-Command invoke-gc -errorAction SilentlyContinue)) {
invoke-gc
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment