Skip to content

Instantly share code, notes, and snippets.

@TylerLeonhardt
Last active November 9, 2018 17:11
Show Gist options
  • Save TylerLeonhardt/3f942e236338a946891679898b8f923d to your computer and use it in GitHub Desktop.
Save TylerLeonhardt/3f942e236338a946891679898b8f923d to your computer and use it in GitHub Desktop.
Show notifications on Linux using PowerShell! Thanks to notify-send(1)
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
)
begin {
$notifySendArgs = @()
if ($Urgency) {
$notifySendArgs += "--urgency=$Urgency"
}
if ($ExpireTime) {
$notifySendArgs += "--expire-time=$ExpireTime"
}
if ($Icon) {
if ($Icon -eq "powershell-logo" -and !(Test-Path "$HOME/.local/share/icons/powershell-logo.png")) {
if (!(Test-Path "$HOME/.local/share/icons")) {
New-Item -ItemType Directory -Path "$HOME/.local/share/icons"
}
Copy-Item "$PSScriptRoot/../powershell-logo.png" "$HOME/.local/share/icons"
}
$notifySendArgs += "--icon=$($Icon -join ',')"
}
if ($Catagory) {
$notifySendArgs += "--category=$($Catagory -join ',')"
}
$notifySendArgs += $Summary
$notifySendArgs += ""
}
process {
$notifySendArgs[$notifySendArgs.Length - 1] = $Body
If ($PSCmdlet.ShouldProcess("notify-send $($notifySendArgs -join ' ')")) {
& "notify-send" $notifySendArgs
}
}
end {}
}
@TylerLeonhardt
Copy link
Author

also on Elementary for some reason you can't specify a path for an icon. It only pulls from the ~/.local/share/icons folder so I added logic to add the PowerShell logo in there. Would love to use a ValidateScript or something to only allow files in that folder.

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