Public/Send-OSNotification.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<#
.SYNOPSIS
Displays toast notifications on Linux, macOS, and Windows.
 
.DESCRIPTION
The Send-OSNotification function displays notification on Linux, macOS, and Windows.
 
Each operating system uses different methods for generating and displaying notifications, but this function abstracts
each of them for a consistent experience regardless of the environment in which it is used.
 
.PARAMETER Body
The main text of your notification. You must specify a body string.
 
.PARAMETER Title
The title of your notification. Generally this is rendered bold at the top of the notification.
 
Defaults to "PowerShell Notification".
 
.PARAMETER Icon
If specified, the specified picture is included on the notification.
 
There is a default Icon included on Windows only.
 
.EXAMPLE
Send-OSNotification -Title 'Script Completed' -Body "The script you were running finished at $(Get-Date -Format t)"
 
This command generates a notification that includes the time at which it was generated in the body.
 
.LINK
https://github.com/Windos/PoshNotify/tree/master/docs/Send-OSNotification.md
 
.NOTES
Notifications are "best effort". Certain features may not be supported on all platforms but PoshNotify will still
attempt to send a notification with as much of the information provided as it can.
 
This function relies on the MacNotify module on macOS, and the PSNotifySend module on Linux.
#>

function Send-OSNotification {
    [CmdletBinding(SupportsShouldProcess=$true,HelpUri="https://github.com/Windos/PoshNotify/tree/master/docs/Send-OSNotification.md")]
    param (
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [string]
        $Body,

        [Parameter()]
        [string]
        $Title = 'PowerShell Notification',

        [Parameter()]
        [string]
        $Icon
    )

    $splat = @{
        Body = $Body
        Title = $Title
    }

    if ($Icon) {
        $splat.Add('Icon', $Icon)
    }

    switch ($true) {
        $IsWindows {
            # Must be PowerShell Core on Windows
            Pop-WindowsNotification @splat
            break
        }
        $IsMacOS {
            Pop-MacOSNotification @splat
            break
        }
        $IsLinux {
            Pop-LinuxNotification @splat
            break
        }
        Default {
            # Must be Windows PowerShell
            Pop-WindowsNotification @splat
            break
        }
    }
}