Last active
November 9, 2018 17:11
-
-
Save TylerLeonhardt/3f942e236338a946891679898b8f923d to your computer and use it in GitHub Desktop.
Show notifications on Linux using PowerShell! Thanks to notify-send(1)
This file contains 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
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 {} | |
} |
The verb should be Send IMHO. Much easier to discover, and much more appropriate for what the command does.
lets all keep in mind that I hacked this together in like 20min 😄
Thanks all for the feedback!
@steviecoaster fixed the typo
@vexx32 switched it to a normal array cause I didn't really want to use iex...
@KirkMunro changed the verb 😄
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
Thinking on it a bit, given this is just adding strings, a StringBuilder would probably be more effective anyway. 🤷♂️