-
-
Save TylerLeonhardt/3f942e236338a946891679898b8f923d to your computer and use it in GitHub Desktop.
| 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 {} | |
| } |
Actually since this code uses += to add to the arraylist, this variable will be converted to a static array after the first operation. You will also need to switch to using the .Add() method.
Thinking on it a bit, given this is just adding strings, a StringBuilder would probably be more effective anyway. π€·ββοΈ
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.
Variable typo
catagoryshould becategoryInstead of an arraylist I'd use an
[System.Collections.Generic.List[]]as they don't produce index output on their .Add() methods