Public/Basic/New-BurntToastNotification.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
function New-BurntToastNotification
{
    <#
        .SYNOPSIS
        Creates and displays a Toast Notification.
         
        .DESCRIPTION
        The New-BurntToastNotification cmdlet creates and displays a Toast Notification on Microsoft Windows 10.
 
        You can specify the text and/or image displayed as well as selecting the sound that is played when the Toast Notification is displayed.
 
        You can optionally call the New-BurntToastNotification cmdlet with the Toast alias.
         
        .INPUTS
        System.String
 
        You cannot pipe input to this cmdlet.
 
        .OUTPUTS
        None
 
        New-BurntToastNotification displays the Toast Notification that is created.
         
        .NOTES
 
        The New-BurntToastNotification function is classified as: Basic
         
        .EXAMPLE
        New-BurntToastNotification
         
        This command creates and displays a Toast Notification with all default values.
         
        .EXAMPLE
        New-BurntToastNotification -Text 'Example Script', 'The example script has run successfully.'
         
        This command creates and displays a Toast Notification with customized title and display text.
 
        .EXAMPLE
        New-BurntToastNotification -Text 'WAKE UP!' -Sound 'Alarm2'
 
        This command creates and displays a Toast Notification which plays a looping alarm sound and lasts longer than a default Toast.
         
        .LINK
        https://github.com/Windos/BurntToast/blob/master/Help/New-BurntToastNotification.md
    #>


    [alias('Toast')]
    [CmdletBinding(DefaultParameterSetName = 'Sound')]
    param
    (
        [Parameter()]
        [ValidateCount(0,3)]
        [String[]] $Text = 'Default Notification',

        [Parameter()]
        #[ValidateScript({ Test-ToastImage -Path $_ })]
        [String] $AppLogo,

        [Parameter()]
        #[ValidateScript({ Test-ToastAppId -Id $_ })]
        [String] $AppId = $Script:Config.AppId,

        [Parameter(ParameterSetName = 'Sound')]
        [ValidateSet('Default',
                     'IM',
                     'Mail',
                     'Reminder',
                     'SMS',
                     'Alarm',
                     'Alarm2',
                     'Alarm3',
                     'Alarm4',
                     'Alarm5',
                     'Alarm6',
                     'Alarm7',
                     'Alarm8',
                     'Alarm9',
                     'Alarm10',
                     'Call',
                     'Call2',
                     'Call3',
                     'Call4',
                     'Call5',
                     'Call6',
                     'Call7',
                     'Call8',
                     'Call9',
                     'Call10')]
        [String] $Sound = 'Default',

        [Parameter(Mandatory = $true,
                   ParameterSetName = 'Silent')]
        [Switch] $Silent,

        [Parameter()]
        [Switch] $SnoozeAndDismiss
    )
    
    $TextObjects = @()

    foreach ($Txt in $Text)
    {
        $TextObjects += New-BTText -Text $Txt
    }

    if ($AppLogo)
    {
        $AppLogoImage = New-BTImage -Source $AppLogo -AppLogoOverride -Crop Circle
    }
    else
    {
        $AppLogoImage = New-BTImage -AppLogoOverride -Crop Circle
    }

    if ($Silent)
    {
        $Audio = New-BTAudio -Silent
    }
    else
    {
        if ($Sound -ne 'Default')
        {     
            if ($Sound -like 'Alarm*' -or $Sound -like 'Call*')
            {
                $Audio = New-BTAudio -Source "ms-winsoundevent:Notification.Looping.$Sound" -Loop
                $Long = $True
            }
            else
            {
                $Audio = New-BTAudio -Source "ms-winsoundevent:Notification.$Sound" -Loop
            }
        }
    }

    $Binding = New-BTBinding -Children $TextObjects -AppLogoOverride $AppLogoImage
    $Visual = New-BTVisual -BindingGeneric $Binding

    if ($SnoozeAndDismiss)
    {
        $SnoozeAction = New-BTAction -SnoozeAndDismiss

        if ($Long)
        {
            $Content = New-BTContent -Audio $Audio -Visual $Visual -Actions $SnoozeAction -Duration Long
        }
        else
        {
            $Content = New-BTContent -Audio $Audio -Visual $Visual -Actions $SnoozeAction
        }
    }
    else
    {
        if ($Long)
        {
            $Content = New-BTContent -Audio $Audio -Visual $Visual -Duration Long
        }
        else
        {
            $Content = New-BTContent -Audio $Audio -Visual $Visual
        }
    }

    Submit-BTNotification -Content $Content -AppId $AppId
}