Skip to content

Instantly share code, notes, and snippets.

@powercode
Last active January 28, 2019 12:53
Show Gist options
  • Save powercode/d1a38bb4c94000064bf9936b59e1706a to your computer and use it in GitHub Desktop.
Save powercode/d1a38bb4c94000064bf9936b59e1706a to your computer and use it in GitHub Desktop.
Send-PSNotification
<#
.SYNOPSIS
Sends a notification on Linux.
.DESCRIPTION
Sends a notification on Linux. Under the hood, this uses notify-send(1)
.PARAMETER Body
The body or main content of the notification.
.PARAMETER Summary
The summary or title of the notification.
.PARAMETER Urgency
Specifies the urgency level (low, normal, critical).
.PARAMETER ExpireTime
Specifies the timeout in milliseconds at which to expire the notification.
.PARAMETER Icon
Specifies an icon filename or stock icon to display.
.PARAMETER Category
Specifies the notification category.
.PARAMETER SoundFile
Specifies the sound file to run when notification is fired.
.EXAMPLE
Send-PSNotification 'Hello World'
.EXAMPLE
Send-PSNotification 'Hello World' -Urgency critical
.EXAMPLE
Send-PSNotification 'Hello World' -Category email,email.arrived
.EXAMPLE
Send-PSNotification 'Hello World' -Icon my-icon
.NOTES
Not all linux distros support ExpireTime. They will fire the notification for a constant duration regardless of what is set.
Not all linux distros support passing a path in for Icon. They will show nothing if not supported. Best practice is to put the icon file in $HOME/.local/share/icons and then specify the name of the file: -Icon my-icon
Not all linux distros support Category. They will show nothing if not supported.
#>
function Send-PSNotification {
[cmdletbinding(SupportsShouldProcess=$true)]
param(
[Parameter(Mandatory,ValueFromPipeline,Position=0)]
[object]
$Body,
[String]
$Summary = 'PowerShell Notification',
[ValidateSet('low', 'normal', 'critical')]
$Urgency,
[int]
$ExpireTime,
[string[]]
$Icon = "powershell-logo",
[ValidateSet("device","device.added","device.error","device.removed",
"email","email.arrived","email.bounced",
"im","im.error","im.received",
"network","network.connected","network.disconnected","network.error",
"presence","presence.offline","presence.online",
"transfer","transfer.complete","transfer.error")]
[string[]]
$Category,
[string]
[ValidateNotNullOrEmpty()]
$SoundFile
)
begin {
if ($Icon -eq "powershell-logo") {
Add-DefaultPSIcon
}
$notifySendArgs = @(
if ($Urgency) { "--urgency=$Urgency" }
if ($ExpireTime) { "--expire-time=$ExpireTime" }
if ($Category) { "--category=$($Category -join ',')" }
if ($SoundFile) { "--hint=string:sound-file:$SoundFile" }
if ($Icon) { "--icon=$($Icon -join ',')" }
$Summary
"" # to be replaced by $Body
)
}
process {
$notifySendArgs[$notifySendArgs.Length - 1] = $Body
If ($PSCmdlet.ShouldProcess("notify-send $($notifySendArgs -join ' ')")) {
& "notify-send" $notifySendArgs
}
}
end {}
}
@powercode
Copy link
Author

This is a sample demonstrating the use of an ArraySubExpression to create the array of arguments passed to the native command.

The use of the if-statements inside the array seems to put some people off, but I think it is a clean way of creating the argument array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment