forked from imabdk/Toast-Notification-Script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-ToastNotification.ps1
More file actions
1375 lines (1286 loc) · 73.2 KB
/
Copy pathNew-ToastNotification.ps1
File metadata and controls
1375 lines (1286 loc) · 73.2 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
<#
.SYNOPSIS
Displays customizable toast notifications on Windows 10/11 systems.
.DESCRIPTION
The Toast Notification Script is a streamlined PowerShell solution for displaying rich toast
notifications to end users on Windows 10/11. It is configurable through an XML configuration file,
allowing organizations to tailor notifications for scenarios such as pending reboots, password
expirations and general information delivery.
The script supports:
- Up to three action buttons with custom actions (reboot, PowerShell scripts, Learn More URLs).
- Dismiss and snooze buttons, with logic to prevent conflicting or excessive button combinations.
- Dynamic content including device uptime and AD password expiration.
- Custom branding with hero and logo images, including downloading images from URLs.
- Integration with PowerShell or a custom notification app for notification delivery.
- Registry and file system checks to ensure prerequisites are met and to prevent excessive frequency.
- Logging of all actions and errors for troubleshooting and auditing.
- Learn More button functionality that opens a URL and re-displays the notification if clicked.
- PowerShell script execution via ToastRunPSScript actions for custom automation tasks.
- Silent execution of custom PowerShell scripts via SilentLauncher.exe to prevent window flashing.
This version focuses on core notification scenarios and removes legacy ConfigMgr integration and OS
upgrade features. The script makes changes to the local machine (registry entries, custom action
scripts and protocols). It must run in the logged on user context (it is designed to be launched by
the scheduled task that Invoke-ToastNotification.ps1 creates); if it detects the SYSTEM or any
non-user context it logs the reason and throws, because toast notifications can only be shown in the
user session.
If a custom PowerShell script (Action3) is configured, the script will automatically download
SilentLauncher.exe to the working directory. The ToastRunPSScript protocol handler is then configured
to use SilentLauncher.exe to execute the target script, ensuring it runs completely silently without
flashing a PowerShell window when the user clicks the action button.
.PARAMETER Config
Path to the XML configuration file. Can be a local path or a URL. If not specified, defaults to
'config-toast.xml' in the script directory.
.EXAMPLE
.\New-ToastNotification.ps1 -Config 'C:\Scripts\config-toast.xml'
Runs the script using the specified local configuration file.
.NOTES
- Requires Windows 10/11.
- Must run as the logged on user, not SYSTEM. The script throws if launched in a non-user context.
- Extensive logging is written to $env:ProgramData\_Automation\Script\New-ToastNotification\ToastNotification.log.
- Learn More button functionality creates temporary files and registry entries for protocol handling.
- If Action3 (Run Script) is used, SilentLauncher.exe is downloaded to execute the script silently.
.LINK
https://github.com/imabdk/Toast-Notification-Script
#>
[CmdletBinding()]
param (
[Parameter(HelpMessage = 'Path to XML Configuration File')]
[String]$Config
)
#region globals
$ProgressPreference = 'SilentlyContinue'
$WarningPreference = 'SilentlyContinue'
$ConfirmPreference = 'None'
#endregion
#region variables
$scriptVersion = '4.0'
$scriptRootPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
$customScriptsPath = '{0}\_Automation\Script\New-ToastNotification' -f $env:ProgramData
$silentLauncherName = 'SilentLauncher'
$silentLauncherPath = '{0}\{1}.exe' -f $customScriptsPath, $silentLauncherName
$baseUrl = 'https://contentrepo.net/repo'
$silentLauncherUrl = '{0}/app/{1}.exe' -f $baseUrl, $silentLauncherName
$registryPath = 'HKCU:\SOFTWARE\ToastNotificationScript'
$defaultUserCulture = 'en-US'
$logoImageTemp = '{0}\ToastLogoImage.jpg' -f $customScriptsPath
$heroImageTemp = '{0}\ToastHeroImage.jpg' -f $customScriptsPath
$imagesPath = 'file:///{0}/New-ToastNotification/Images' -f $customScriptsPath
$learnMoreLogPath = '{0}\learn-more.txt' -f $customScriptsPath
#endregion
#region functions
function Write-ToastLog {
<#
.SYNOPSIS
Writes a message to a log file with a specified level.
.DESCRIPTION
Logs messages to a file, supporting Info, Warn and Error levels. If the log file exceeds 5MB it
is deleted and recreated. Creates the log file if it does not exist.
.PARAMETER Message
The message to be logged.
.PARAMETER Path
The path to the log file. Defaults to the ToastNotification.log in the custom scripts path.
.PARAMETER Level
The log level: Info, Warn or Error. Defaults to Info.
.EXAMPLE
Write-ToastLog -Message 'Script started' -Level 'Info'
Writes an informational log entry.
#>
[CmdletBinding()]
[OutputType([System.Void])]
param (
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[Alias('LogContent')]
[String]$Message,
[Parameter(Mandatory = $false)]
[Alias('LogPath')]
[String]$Path = ('{0}\ToastNotification.log' -f $customScriptsPath),
[Parameter(Mandatory = $false)]
[ValidateSet('Error', 'Warn', 'Info')]
[String]$Level = 'Info'
)
begin { }
process {
$maxLogSize = 5
if (Test-Path -Path $Path) {
$logSize = (Get-Item -Path $Path).Length / 1MB
}
if ((Test-Path -Path $Path) -and $logSize -gt $maxLogSize) {
Write-Error -Message ('Log file {0} already exists and file exceeds maximum file size. Deleting the log and starting fresh.' -f $Path)
Remove-Item -Path $Path -Force
New-Item -Path $Path -Force -ItemType File | Out-Null
} elseif (-not (Test-Path -Path $Path)) {
Write-Verbose -Message ('Creating {0}.' -f $Path)
New-Item -Path $Path -Force -ItemType File | Out-Null
}
$formattedDate = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
switch ($Level) {
'Error' {
Write-Error -Message $Message
$levelText = 'ERROR:'
}
'Warn' {
Write-Warning -Message $Message
$levelText = 'WARNING:'
}
'Info' {
Write-Verbose -Message $Message
$levelText = 'INFO:'
}
}
('{0} {1} {2}' -f $formattedDate, $levelText, $Message) | Out-File -FilePath $Path -Append
}
end { }
}
function Test-PendingRebootRegistry {
<#
.SYNOPSIS
Checks for pending reboots in the registry.
.DESCRIPTION
Examines specific registry keys to determine if a reboot is pending due to component-based
servicing or Windows Update operations.
.EXAMPLE
Test-PendingRebootRegistry
Returns True when a reboot is pending.
#>
[CmdletBinding()]
[OutputType([Bool])]
param ()
Write-ToastLog -Message 'Running Test-PendingRebootRegistry function'
$cbsRebootKey = Test-Path -Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending'
$wuRebootKey = Test-Path -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired'
if (($cbsRebootKey) -or ($wuRebootKey)) {
Write-ToastLog -Message 'Check returned TRUE on ANY of the registry checks: Reboot is pending!'
return $true
} else {
Write-ToastLog -Message 'Check returned FALSE on ANY of the registry checks: Reboot is NOT pending!'
return $false
}
}
function Get-DeviceUptime {
<#
.SYNOPSIS
Retrieves the device uptime in days.
.DESCRIPTION
Calculates the number of days since the last system boot using CIM.
.EXAMPLE
Get-DeviceUptime
Returns the number of days since the last boot.
#>
[CmdletBinding()]
[OutputType([Int])]
param ()
Write-ToastLog -Message 'Running Get-DeviceUptime function'
$operatingSystem = Get-CimInstance -ClassName Win32_OperatingSystem
$uptime = (Get-Date) - ($operatingSystem.LastBootUpTime)
return $uptime.Days
}
function Get-WindowsVersion {
<#
.SYNOPSIS
Verifies if the system is running a supported Windows version.
.DESCRIPTION
Checks if the OS is Windows 10 or 11 and a workstation type. Returns True if supported.
.EXAMPLE
Get-WindowsVersion
Returns True on a supported workstation OS.
#>
[CmdletBinding()]
[OutputType([Bool])]
param ()
$operatingSystem = Get-CimInstance -ClassName Win32_OperatingSystem
if (($operatingSystem.Version -like '10.0.*') -and ($operatingSystem.ProductType -eq 1)) {
Write-ToastLog -Message 'Running supported version of Windows. Windows 10 and workstation OS detected'
return $true
} else {
Write-ToastLog -Level Error -Message 'Not running supported version of Windows'
return $false
}
}
function Test-WindowsPushNotificationsEnabled {
<#
.SYNOPSIS
Tests if Windows push notifications are enabled for the user.
.DESCRIPTION
Checks the registry to see if toast notifications are enabled for the current user. Returns True
when enabled and False when disabled or when the value is missing.
.EXAMPLE
Test-WindowsPushNotificationsEnabled
Returns True when toast notifications are enabled.
#>
[CmdletBinding()]
[OutputType([Bool])]
param ()
$toastEnabledKey = (Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\PushNotifications' -Name ToastEnabled -ErrorAction Ignore).ToastEnabled
if ($toastEnabledKey -eq '1') {
Write-ToastLog -Message 'Toast notifications for the logged on user are enabled in Windows'
return $true
} else {
Write-ToastLog -Level Error -Message 'Toast notifications for the logged on user are not enabled in Windows. The script will try to enable toast notifications for the logged on user'
return $false
}
}
function Enable-WindowsPushNotification {
<#
.SYNOPSIS
Enables Windows push notifications for the user.
.DESCRIPTION
Modifies the registry and restarts the Windows Push Notification service to enable toast notifications.
.EXAMPLE
Enable-WindowsPushNotification
Enables toast notifications for the logged on user.
#>
[CmdletBinding()]
[OutputType([System.Void])]
param ()
$toastEnabledKeyPath = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\PushNotifications'
Write-ToastLog -Message 'Trying to enable toast notifications for the logged on user'
try {
Set-ItemProperty -Path $toastEnabledKeyPath -Name ToastEnabled -Value 1 -Force
Get-Service -Name 'WpnUserService*' | Restart-Service -Force
Write-ToastLog -Message 'Successfully enabled toast notifications for the logged on user'
} catch {
Write-ToastLog -Level Error -Message 'Failed to enable toast notifications for the logged on user. Toast notifications will probably not be displayed'
}
}
function Test-NTSystem {
<#
.SYNOPSIS
Determines if the script is running under the SYSTEM account.
.DESCRIPTION
Checks the current security principal to identify if the script is running as SYSTEM or a user.
.EXAMPLE
Test-NTSystem
Returns True when running as SYSTEM.
#>
[CmdletBinding()]
[OutputType([Bool])]
param ()
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
if ($currentUser.IsSystem -eq $true) {
Write-ToastLog -Message 'Script is initially running in SYSTEM context. Please be vary, that this has limitations and may not work!'
return $true
} else {
Write-ToastLog -Message 'Script is initially running in USER context'
return $false
}
}
function Get-GivenName {
<#
.SYNOPSIS
Retrieves the user given name.
.DESCRIPTION
Attempts to get the user given name from Active Directory, falling back to registry data if AD
is unavailable.
.EXAMPLE
Get-GivenName
Returns the given name of the logged on user.
#>
[CmdletBinding()]
[OutputType([String])]
param ()
Write-ToastLog -Message 'Running Get-GivenName function'
try {
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$principalContext = [System.DirectoryServices.AccountManagement.PrincipalContext]::new([System.DirectoryServices.AccountManagement.ContextType]::Domain, [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain())
$givenName = ([System.DirectoryServices.AccountManagement.Principal]::FindByIdentity($principalContext, [System.DirectoryServices.AccountManagement.IdentityType]::SamAccountName, [Environment]::UserName)).GivenName
$principalContext.Dispose()
} catch [System.Exception] {
Write-ToastLog -Level Warn -Message ('{0}' -f $_)
}
if (-not [string]::IsNullOrEmpty($givenName)) {
Write-ToastLog -Message ('Given name retrieved from Active Directory: {0}' -f $givenName)
return $givenName
} else {
Write-ToastLog -Message 'Given name not found in AD or no local AD is available. Continuing looking for given name elsewhere'
$regKey = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI'
if ((Get-ItemProperty -Path $regKey).LastLoggedOnDisplayName) {
$loggedOnUserDisplayName = Get-ItemProperty -Path $regKey -Name 'LastLoggedOnDisplayName' | Select-Object -ExpandProperty LastLoggedOnDisplayName
if (-not [string]::IsNullOrEmpty($loggedOnUserDisplayName)) {
$displayName = $loggedOnUserDisplayName.Split(' ')
$givenName = $displayName[0]
Write-ToastLog -Message ('Given name found directly in registry: {0}' -f $givenName)
return $givenName
} else {
Write-ToastLog -Message 'Given name not found in registry. Using nothing as placeholder'
return $null
}
} else {
Write-ToastLog -Message 'Given name not found in registry. Using nothing as placeholder'
return $null
}
}
}
function Get-ADPasswordExpiration {
<#
.SYNOPSIS
Checks if the user AD password is nearing expiration.
.DESCRIPTION
Queries AD to determine the password expiration date and compares it against a threshold in days.
.PARAMETER FADPasswordExpirationDays
The number of days within which to check for password expiration.
.EXAMPLE
Get-ADPasswordExpiration -FADPasswordExpirationDays '14'
Returns True with the expiry date and time span when the password expires within 14 days.
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword', '')]
[CmdletBinding()]
[OutputType([System.Object[]])]
param (
[Parameter(Mandatory = $true)]
[String]$FADPasswordExpirationDays
)
Write-ToastLog -Message 'Running Get-ADPasswordExpiration function'
try {
Write-ToastLog -Message 'Looking up SamAccountName and DomainName in local Active Directory'
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$principalContext = [System.DirectoryServices.AccountManagement.PrincipalContext]::new([System.DirectoryServices.AccountManagement.ContextType]::Domain, [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain())
$samAccountName = ([System.DirectoryServices.AccountManagement.Principal]::FindByIdentity($principalContext, [System.DirectoryServices.AccountManagement.IdentityType]::SamAccountName, [Environment]::UserName)).SamAccountName
$domainName = ([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).Name
$principalContext.Dispose()
} catch [System.Exception] {
Write-ToastLog -Level Error -Message ('{0}' -f $_)
}
if (($samAccountName) -and ($domainName)) {
Write-ToastLog -Message ('SamAccountName found: {0} and DomainName found: {1}. Continuing looking for AD password expiration date' -f $samAccountName, $domainName)
try {
$root = [ADSI] ('LDAP://{0}' -f $domainName)
$searcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher -ArgumentList $root, ('(SamAccountName = {0})' -f $samAccountName)
$searcher.PropertiesToLoad.Add('msDS-UserPasswordExpiryTimeComputed') | Out-Null
$result = $searcher.FindOne()
$expiryDate = [DateTime]::FromFileTime([Int64]::Parse((($result.Properties['msDS-UserPasswordExpiryTimeComputed'])[0]).ToString()))
} catch {
Write-ToastLog -Level Error -Message 'Failed to retrieve password expiration date from Active Directory. Script is continuing, but without password expiration date'
}
if ($expiryDate) {
Write-ToastLog -Message ('Password expiration date found. Password is expiring on {0}. Calculating time to expiration' -f $expiryDate)
$localCulture = Get-Culture
$regionDateFormat = [System.Globalization.CultureInfo]::GetCultureInfo($localCulture.LCID).DateTimeFormat.LongDatePattern
$expiryDate = Get-Date -Date $expiryDate -Format $regionDateFormat
$today = Get-Date -Format $regionDateFormat
$dateDiff = New-TimeSpan -Start $today -End $expiryDate
if ($dateDiff.Days -le $FADPasswordExpirationDays -and $dateDiff.Days -ge 0) {
Write-ToastLog -Message 'Password is expiring within the set period. Returning True'
Write-ToastLog -Message ('ADPasswordExpirationDays is set to: {0}' -f $FADPasswordExpirationDays)
return @($true, $expiryDate, $dateDiff)
} else {
Write-ToastLog -Message 'Password is not expiring anytime soon. Returning False'
Write-ToastLog -Message ('ADPasswordExpirationDays is set to: {0}' -f $FADPasswordExpirationDays)
return @($false)
}
} else {
Write-ToastLog -Level Error -Message 'No password expiration date found. Returning False'
return @($false)
}
} else {
Write-ToastLog -Level Error -Message 'Failed to retrieve SamAccountName or DomainName from local Active Directory. Script is continuing, but password expiration date cannot be retrieved'
return @($false)
}
}
function Write-CustomActionRegistry {
<#
.SYNOPSIS
Registers custom action protocols in the registry.
.DESCRIPTION
Creates registry entries for custom protocols (for example ToastReboot) used by toast action buttons.
Points the protocol handler to SilentLauncher.exe to execute the .cmd files silently and guarantee
zero window flashing at the OS level.
.PARAMETER ActionType
The type of action to register (for example ToastReboot).
.PARAMETER RegCommandPath
The path where the command script is located. Defaults to the custom scripts path.
.EXAMPLE
Write-CustomActionRegistry -ActionType 'ToastReboot'
Registers the ToastReboot protocol.
#>
[CmdletBinding()]
[OutputType([System.Void])]
param (
[Parameter(Position = 0)]
[ValidateSet('ToastReboot', 'ToastRunPSScript', 'ToastLearnMore')]
[String]$ActionType,
[Parameter(Position = 1)]
[String]$RegCommandPath = $customScriptsPath
)
Write-ToastLog -Message ('Running Write-CustomActionRegistry function: {0}' -f $ActionType)
try {
New-Item -Path ('HKCU:\Software\Classes\{0}\shell\open\command' -f $ActionType) -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath ('HKCU:\Software\Classes\{0}' -f $ActionType) -Name 'URL Protocol' -Value '' -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath ('HKCU:\Software\Classes\{0}' -f $ActionType) -Name '(default)' -Value ('URL:{0} Protocol' -f $ActionType) -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
$silentLauncherPath = '{0}\SilentLauncher.exe' -f $RegCommandPath
$cmdFilePath = '{0}\{1}.cmd' -f $RegCommandPath, $ActionType
if ($ActionType -eq 'ToastRunPSScript') {
$regCommandValue = '{0}{1}{0} -script {0}{2}{0} %1' -f [char]34, $silentLauncherPath, $cmdFilePath
} else {
$regCommandValue = '{0}{1}{0} -script {0}{2}{0}' -f [char]34, $silentLauncherPath, $cmdFilePath
}
New-ItemProperty -LiteralPath ('HKCU:\Software\Classes\{0}\shell\open\command' -f $ActionType) -Name '(default)' -Value $regCommandValue -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
} catch {
Write-ToastLog -Level Error -Message ('Failed to create the {0} custom protocol in HKCU\Software\Classes. Action button might not work. Reason: {1}' -f $ActionType, $_.Exception.Message)
}
}
function Write-CustomActionScript {
<#
.SYNOPSIS
Creates scripts for custom actions triggered by toast buttons.
.DESCRIPTION
Generates the .cmd and .ps1 scripts for actions like rebooting, running the toast as the user,
running a PowerShell script or opening a Learn More URL, storing them in the specified path.
.PARAMETER Type
The type of action script to create (for example ToastReboot).
.PARAMETER Path
The directory where scripts are saved. Defaults to the custom scripts path.
.EXAMPLE
Write-CustomActionScript -Type 'ToastReboot'
Creates the ToastReboot command script.
#>
[CmdletBinding()]
[OutputType([System.Void])]
param (
[Parameter(Position = 0)]
[ValidateSet('ToastReboot', 'ToastRunPSScript', 'ToastLearnMore')]
[String]$Type,
[Parameter(Position = 1)]
[String]$Path = $customScriptsPath
)
Write-ToastLog -Message ('Running Write-CustomActionScript function: {0}' -f $Type)
switch ($Type) {
'ToastReboot' {
try {
$cmdFileName = '{0}.cmd' -f $Type
New-Item -Path $Path -Name $cmdFileName -Force -OutVariable pathInfo | Out-Null
$getCustomScriptPath = $pathInfo.FullName
$scriptContent = 'shutdown /r /t 0 /d p:0:0 /c {0}Toast Notification Reboot{0}' -f [char]34
if (-not [string]::IsNullOrEmpty($scriptContent)) {
Out-File -FilePath $getCustomScriptPath -InputObject $scriptContent -Encoding ASCII -Force
}
} catch {
Write-ToastLog -Level Error -Message ('Failed to create the custom .cmd script for {0}. Action button might not work' -f $Type)
Write-ToastLog -Level Error -Message ('Error message: {0}' -f $_.Exception.Message)
}
break
}
'ToastRunPSScript' {
try {
$cmdFileName = '{0}.cmd' -f $Type
New-Item -Path $Path -Name $cmdFileName -Force -OutVariable pathInfo | Out-Null
$getCustomScriptPath = $pathInfo.FullName
$scriptContent = '{0} -script {1}{2}{1}' -f $silentLauncherPath, [char]34, $psScriptPath
if (-not [string]::IsNullOrEmpty($scriptContent)) {
Out-File -FilePath $getCustomScriptPath -InputObject $scriptContent -Encoding ASCII -Force
}
} catch {
Write-ToastLog -Level Error -Message ('Failed to create the custom .cmd script for {0}. Action button might not work' -f $Type)
Write-ToastLog -Level Error -Message ('Error message: {0}' -f $_.Exception.Message)
}
break
}
'ToastLearnMore' {
try {
$cmdFileName = '{0}.cmd' -f $Type
New-Item -Path $Path -Name $cmdFileName -Force -OutVariable pathInfo | Out-Null
$getCustomScriptPath = $pathInfo.FullName
$learnMoreTemplate = @'
@echo off
start {1}
set {0}learnMoreUrl={1}{0}
set {0}logFile={2}{0}
echo [%date% %time%] URL [%learnMoreUrl%] launched >> {0}%logFile%{0}
'@
$scriptContent = $learnMoreTemplate -f [char]34, [String]$learnMoreUrl, [String]$learnMoreLogPath
if (-not [string]::IsNullOrEmpty($scriptContent)) {
Out-File -FilePath $getCustomScriptPath -InputObject $scriptContent -Encoding ASCII -Force
}
} catch {
Write-ToastLog -Level Error -Message ('Failed to create the custom .cmd script for {0}. Action button might not work' -f $Type)
Write-ToastLog -Level Error -Message ('Error message: {0}' -f $_.Exception.Message)
}
break
}
}
}
function Show-ToastNotification {
<#
.SYNOPSIS
Displays the toast notification to the user.
.DESCRIPTION
Shows the constructed toast notification in the logged on user context and optionally plays
custom audio.
.EXAMPLE
Show-ToastNotification
Displays the toast notification.
#>
[CmdletBinding()]
[OutputType([System.Void])]
param ()
try {
Write-ToastLog -Message 'Confirmed USER context before displaying toast'
$null = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
$null = [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]
$toastXml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
$toastXml.LoadXml($Toast.OuterXml)
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($App).Show($toastXml)
Write-ToastLog -Message 'All good. Toast notification was displayed'
Write-Information -MessageData 'All good. Toast notification was displayed' -InformationAction Continue
if ($CustomAudio -eq 'True') {
Add-Type -AssemblyName System.Speech
$speak = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
Start-Sleep -Seconds 1.25
$speak.SelectVoiceByHints('Female', 65)
$speak.Speak($CustomAudioTextToSpeech)
$speak.Dispose()
}
Save-NotificationLastRunTime
} catch {
Write-ToastLog -Message 'Something went wrong when displaying the toast notification' -Level Error
Write-ToastLog -Message 'Make sure the script is running as the logged on user' -Level Error
Write-Information -MessageData 'Something went wrong when displaying the toast notification. Make sure the script is running as the logged on user' -InformationAction Continue
}
}
function Get-NotificationLastRunTime {
<#
.SYNOPSIS
Retrieves the time since the last toast notification was displayed.
.DESCRIPTION
Reads the last run time from the registry and calculates the minutes elapsed since then.
.EXAMPLE
Get-NotificationLastRunTime
Returns the number of minutes since the toast was last displayed.
#>
[CmdletBinding()]
[OutputType([Int])]
param ()
$lastRunTime = (Get-ItemProperty -Path $registryPath -Name LastRunTime -ErrorAction Ignore).LastRunTime
$currentTime = Get-Date -Format s
if (-not [string]::IsNullOrEmpty($lastRunTime)) {
$difference = ([datetime]$currentTime - [datetime]$lastRunTime)
$minutesSinceLastRunTime = [math]::Round($difference.TotalMinutes)
Write-ToastLog -Message ('Toast notification was previously displayed {0} minutes ago' -f $minutesSinceLastRunTime)
return $minutesSinceLastRunTime
}
}
function Save-NotificationLastRunTime {
<#
.SYNOPSIS
Saves the current time as the last run time of the toast notification.
.DESCRIPTION
Stores the current timestamp in the registry to track when the toast was last shown.
.EXAMPLE
Save-NotificationLastRunTime
Saves the current time to the registry.
#>
[CmdletBinding()]
[OutputType([System.Void])]
param ()
$runTime = Get-Date -Format s
if (-not (Get-ItemProperty -Path $registryPath -Name LastRunTime -ErrorAction Ignore)) {
New-ItemProperty -Path $registryPath -Name LastRunTime -Value $runTime -Force | Out-Null
} else {
Set-ItemProperty -Path $registryPath -Name LastRunTime -Value $runTime -Force | Out-Null
}
}
function Register-CustomNotificationApp {
<#
.SYNOPSIS
Registers a custom notification app in the registry.
.DESCRIPTION
Creates registry entries to define a custom app for displaying toast notifications.
.PARAMETER FAppID
The ID of the custom app.
.PARAMETER FAppDisplayName
The display name of the custom app.
.EXAMPLE
Register-CustomNotificationApp -FAppID 'Toast.Custom.App' -FAppDisplayName 'Custom Toast App'
Registers a custom notification app.
#>
[CmdletBinding()]
[OutputType([System.Void])]
param (
[Parameter(Mandatory = $true)]
[String]$FAppID,
[Parameter(Mandatory = $true)]
[String]$FAppDisplayName
)
Write-ToastLog -Message 'Running Register-NotificationApp function'
$appID = $FAppID
$appDisplayName = $FAppDisplayName
[int]$showInSettings = 0
[int]$iconBackgroundColor = 0
$iconUri = '%SystemRoot%\ImmersiveControlPanel\images\logo.png'
$appRegPath = 'HKCU:\Software\Classes\AppUserModelId'
$regPath = '{0}\{1}' -f $appRegPath, $appID
try {
if (-not (Test-Path -Path $regPath)) {
New-Item -Path $appRegPath -Name $appID -Force | Out-Null
}
$displayName = Get-ItemProperty -Path $regPath -Name DisplayName -ErrorAction SilentlyContinue | Select-Object -ExpandProperty DisplayName -ErrorAction SilentlyContinue
if ($displayName -ne $appDisplayName) {
New-ItemProperty -Path $regPath -Name DisplayName -Value $appDisplayName -PropertyType String -Force | Out-Null
}
$showInSettingsValue = Get-ItemProperty -Path $regPath -Name ShowInSettings -ErrorAction SilentlyContinue | Select-Object -ExpandProperty ShowInSettings -ErrorAction SilentlyContinue
if ($showInSettingsValue -ne $showInSettings) {
New-ItemProperty -Path $regPath -Name ShowInSettings -Value $showInSettings -PropertyType DWORD -Force | Out-Null
}
$iconUriValue = Get-ItemProperty -Path $regPath -Name IconUri -ErrorAction SilentlyContinue | Select-Object -ExpandProperty IconUri -ErrorAction SilentlyContinue
if ($iconUriValue -ne $iconUri) {
New-ItemProperty -Path $regPath -Name IconUri -Value $iconUri -PropertyType ExpandString -Force | Out-Null
}
$iconBackgroundColorValue = Get-ItemProperty -Path $regPath -Name IconBackgroundColor -ErrorAction SilentlyContinue | Select-Object -ExpandProperty IconBackgroundColor -ErrorAction SilentlyContinue
if ($iconBackgroundColorValue -ne $iconBackgroundColor) {
New-ItemProperty -Path $regPath -Name IconBackgroundColor -Value $iconBackgroundColor -PropertyType ExpandString -Force | Out-Null
}
Write-ToastLog -Message ('Created registry entries for custom notification app: {0}' -f $FAppDisplayName)
} catch {
Write-ToastLog -Message 'Failed to create one or more registry entries for the custom notification app' -Level Error
Write-ToastLog -Message 'Toast Notifications are usually not displayed if the notification app does not exist' -Level Error
}
}
#endregion
#region initialization
$userCulture = try {
(Get-Culture).Name
} catch {
Write-ToastLog -Level Error -Message 'Failed to get users local culture. This is used with the multi-language option, which now might not work properly'
}
if (-not (Test-Path -Path $registryPath)) {
Write-ToastLog -Message ('ToastNotificationScript registry path not found. Creating it: {0}' -f $registryPath)
try {
New-Item -Path $registryPath -Force | Out-Null
} catch {
Write-ToastLog -Message ('Failed to create the ToastNotificationScript registry path: {0}' -f $registryPath) -Level Error
Write-ToastLog -Message 'This is required. Script will now exit' -Level Error
exit 1
}
}
if (-not (Test-Path -Path $customScriptsPath)) {
Write-ToastLog -Message ('CustomScriptPath not found. Creating it: {0}' -f $customScriptsPath)
try {
New-Item -Path $customScriptsPath -ItemType Directory -Force | Out-Null
} catch {
Write-ToastLog -Level Error -Message ('Failed to create the CustomScriptPath folder: {0}' -f $customScriptsPath)
Write-ToastLog -Message 'This is required. Script will now exit' -Level Error
exit 1
}
}
$supportedWindowsVersion = Get-WindowsVersion
if ($supportedWindowsVersion -eq $false) {
Write-ToastLog -Message 'Aborting script' -Level Error
exit 1
}
$isSystem = Test-NTSystem
if ($isSystem -eq $true) {
Write-ToastLog -Level Error -Message 'This script is running in the SYSTEM (non-user) context. Toast notifications can only be displayed in the logged on user session'
Write-ToastLog -Level Error -Message 'This script must be launched as the logged on user (for example through the scheduled task created by Invoke-ToastNotification.ps1). Aborting'
throw 'New-ToastNotification.ps1 must run in the logged on user context, not as SYSTEM.'
}
$windowsPushNotificationsEnabled = Test-WindowsPushNotificationsEnabled
if ($windowsPushNotificationsEnabled -eq $false) {
Enable-WindowsPushNotification
}
#endregion
#region configuration loading
if (-not $Config) {
Write-ToastLog -Message 'No config file set as parameter. Using local config file'
$Config = Join-Path -Path $scriptRootPath -ChildPath 'config-toast.xml'
}
if ($Config.StartsWith('https://') -or $Config.StartsWith('http://')) {
Write-ToastLog -Message 'Specified config file seems hosted [online]. Treating it accordingly'
try { $testOnlineConfig = Invoke-WebRequest -Uri $Config -UseBasicParsing } catch { $null }
if ($testOnlineConfig.StatusDescription -eq 'OK') {
try {
$webClient = New-Object -TypeName System.Net.WebClient
$webClient.Encoding = [System.Text.Encoding]::UTF8
$xml = [xml]$webClient.DownloadString($Config)
Write-ToastLog -Message ('Successfully loaded {0}' -f $Config)
} catch {
Write-ToastLog -Message ('Error, could not read {0}' -f $Config) -Level Error
Write-ToastLog -Message ('Error message: {0}' -f $_.Exception.Message) -Level Error
Write-Information -MessageData ('Error, could not read {0}. Error message: {1}' -f $Config, $_.Exception.Message) -InformationAction Continue
exit 1
}
} else {
Write-ToastLog -Level Error -Message 'The provided URL to the config does not reply or does not come back OK'
Write-Information -MessageData 'The provided URL to the config does not reply or does not come back OK' -InformationAction Continue
exit 1
}
} else {
Write-ToastLog -Message 'Specified config file seems hosted [locally or fileshare]. Treating it accordingly'
if (Test-Path -Path $Config) {
try {
$xml = [xml](Get-Content -Path $Config -Encoding UTF8)
Write-ToastLog -Message ('Successfully loaded {0}' -f $Config)
} catch {
Write-ToastLog -Message ('Error, could not read {0}' -f $Config) -Level Error
Write-ToastLog -Message ('Error message: {0}' -f $_.Exception.Message) -Level Error
exit 1
}
} else {
Write-ToastLog -Level Error -Message 'No config file found on the specified location [locally or fileshare]'
exit 1
}
}
if (-not [string]::IsNullOrEmpty($xml)) {
try {
Write-ToastLog -Message ('Loading xml content from {0} into variables' -f $Config)
$toastEnabled = $xml.Configuration.Feature | Where-Object -FilterScript { $_.Name -like 'Toast' } | Select-Object -ExpandProperty 'Enabled'
$pendingRebootUptime = $xml.Configuration.Feature | Where-Object -FilterScript { $_.Name -like 'PendingRebootUptime' } | Select-Object -ExpandProperty 'Enabled'
$pendingRebootCheck = $xml.Configuration.Feature | Where-Object -FilterScript { $_.Name -like 'PendingRebootCheck' } | Select-Object -ExpandProperty 'Enabled'
$aDPasswordExpiration = $xml.Configuration.Feature | Where-Object -FilterScript { $_.Name -like 'ADPasswordExpiration' } | Select-Object -ExpandProperty 'Enabled'
$pendingRebootUptimeTextEnabled = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'PendingRebootUptimeText' } | Select-Object -ExpandProperty 'Enabled'
$maxUptimeDays = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'MaxUptimeDays' } | Select-Object -ExpandProperty 'Value'
$pendingRebootCheckTextEnabled = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'PendingRebootCheckText' } | Select-Object -ExpandProperty 'Enabled'
$aDPasswordExpirationTextEnabled = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'ADPasswordExpirationText' } | Select-Object -ExpandProperty 'Enabled'
$aDPasswordExpirationDays = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'ADPasswordExpirationDays' } | Select-Object -ExpandProperty 'Value'
$deadlineEnabled = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'Deadline' } | Select-Object -ExpandProperty 'Enabled'
$deadlineContent = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'Deadline' } | Select-Object -ExpandProperty 'Value'
$dynDeadlineEnabled = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'DynamicDeadline' } | Select-Object -ExpandProperty 'Enabled'
$createScriptsProtocolsEnabled = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'CreateScriptsAndProtocols' } | Select-Object -ExpandProperty 'Enabled'
$limitToastToRunEveryMinutesEnabled = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'LimitToastToRunEveryMinutes' } | Select-Object -ExpandProperty 'Enabled'
$limitToastToRunEveryMinutesValue = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'LimitToastToRunEveryMinutes' } | Select-Object -ExpandProperty 'Value'
$customAppEnabled = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'CustomNotificationApp' } | Select-Object -ExpandProperty 'Enabled'
$customAppValue = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'CustomNotificationApp' } | Select-Object -ExpandProperty 'Value'
$psAppStatus = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'UsePowershellApp' } | Select-Object -ExpandProperty 'Enabled'
$customAudio = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'CustomAudio' } | Select-Object -ExpandProperty 'Enabled'
$logoImageFileName = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'LogoImageName' } | Select-Object -ExpandProperty 'Value'
$heroImageFileName = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'HeroImageName' } | Select-Object -ExpandProperty 'Value'
if (($logoImageFileName -match [Regex]::Escape(':\'))) {
$logoImage = $logoImageFileName
}
if (($heroImageFileName -match [Regex]::Escape(':\'))) {
$heroImage = $heroImageFileName
}
if ((-not [string]::IsNullOrEmpty($logoImageFileName)) -and ([string]::IsNullOrEmpty($logoImage))) {
$logoImage = '{0}/{1}' -f $imagesPath, $logoImageFileName
}
if ((-not [string]::IsNullOrEmpty($heroImageFileName)) -and ([string]::IsNullOrEmpty($heroImage))) {
$heroImage = '{0}/{1}' -f $imagesPath, $heroImageFileName
}
$scenario = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'Scenario' } | Select-Object -ExpandProperty 'Type'
$action1 = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'Action1' } | Select-Object -ExpandProperty 'Value'
$action2 = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'Action2' } | Select-Object -ExpandProperty 'Value'
$action3 = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'Action3' } | Select-Object -ExpandProperty 'Value'
$greetGivenName = $xml.Configuration.Text | Where-Object -FilterScript { $_.Option -like 'GreetGivenName' } | Select-Object -ExpandProperty 'Enabled'
$multiLanguageSupport = $xml.Configuration.Text | Where-Object -FilterScript { $_.Option -like 'MultiLanguageSupport' } | Select-Object -ExpandProperty 'Enabled'
$actionButton1Enabled = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'ActionButton1' } | Select-Object -ExpandProperty 'Enabled'
$actionButton2Enabled = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'ActionButton2' } | Select-Object -ExpandProperty 'Enabled'
$actionButton3Enabled = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'ActionButton3' } | Select-Object -ExpandProperty 'Enabled'
$dismissButtonEnabled = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'DismissButton' } | Select-Object -ExpandProperty 'Enabled'
$snoozeButtonEnabled = $xml.Configuration.Option | Where-Object -FilterScript { $_.Name -like 'SnoozeButton' } | Select-Object -ExpandProperty 'Enabled'
if ($multiLanguageSupport -eq 'True') {
Write-ToastLog -Message ('MultiLanguageSupport set to True. Current language culture is {0}. Checking for language support' -f $userCulture)
if (-not [string]::IsNullOrEmpty($xml.Configuration.$userCulture)) {
Write-ToastLog -Message ('Support for the users language culture found, localizing text using {0}' -f $userCulture)
$xmlLang = $xml.Configuration.$userCulture
} elseif (-not [string]::IsNullOrEmpty($xml.Configuration.$defaultUserCulture)) {
Write-ToastLog -Message ('No support for the users language culture found, using {0} as default fallback language' -f $defaultUserCulture)
$xmlLang = $xml.Configuration.$defaultUserCulture
}
} else {
$xmlLang = $xml.Configuration.$defaultUserCulture
}
$pendingRebootUptimeTextValue = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'PendingRebootUptimeText' } | Select-Object -ExpandProperty '#text'
$pendingRebootCheckTextValue = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'PendingRebootCheckText' } | Select-Object -ExpandProperty '#text'
$aDPasswordExpirationTextValue = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'ADPasswordExpirationText' } | Select-Object -ExpandProperty '#text'
$customAudioTextToSpeech = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'CustomAudioTextToSpeech' } | Select-Object -ExpandProperty '#text'
$actionButton1Content = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'ActionButton1' } | Select-Object -ExpandProperty '#text'
$actionButton2Content = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'ActionButton2' } | Select-Object -ExpandProperty '#text'
$actionButton3Content = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'ActionButton3' } | Select-Object -ExpandProperty '#text'
$dismissButtonContent = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'DismissButton' } | Select-Object -ExpandProperty '#text'
$snoozeButtonContent = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'SnoozeButton' } | Select-Object -ExpandProperty '#text'
$attributionText = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'AttributionText' } | Select-Object -ExpandProperty '#text'
$headerText = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'HeaderText' } | Select-Object -ExpandProperty '#text'
$titleText = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'TitleText' } | Select-Object -ExpandProperty '#text'
$bodyText1 = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'BodyText1' } | Select-Object -ExpandProperty '#text'
$bodyText2 = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'BodyText2' } | Select-Object -ExpandProperty '#text'
$snoozeText = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'SnoozeText' } | Select-Object -ExpandProperty '#text'
$deadlineText = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'DeadlineText' } | Select-Object -ExpandProperty '#text'
$greetMorningText = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'GreetMorningText' } | Select-Object -ExpandProperty '#text'
$greetAfternoonText = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'GreetAfternoonText' } | Select-Object -ExpandProperty '#text'
$greetEveningText = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'GreetEveningText' } | Select-Object -ExpandProperty '#text'
$minutesText = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'MinutesText' } | Select-Object -ExpandProperty '#text'
$hourText = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'HourText' } | Select-Object -ExpandProperty '#text'
$hoursText = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'HoursText' } | Select-Object -ExpandProperty '#text'
$computerUptimeText = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'ComputerUptimeText' } | Select-Object -ExpandProperty '#text'
$computerUptimeDaysText = $xmlLang.Text | Where-Object -FilterScript { $_.Name -like 'ComputerUptimeDaysText' } | Select-Object -ExpandProperty '#text'
Write-ToastLog -Message ('Successfully loaded xml content from {0}' -f $Config)
} catch {
Write-ToastLog -Message ('Xml content from {0} was not loaded properly. Reason: {1}' -f $Config, $_.Exception.Message)
exit 1
}
}
#endregion
#region validation
if ($toastEnabled -ne 'True') {
Write-ToastLog -Message ('Toast notification is not enabled. Please check {0} file' -f $Config)
exit 1
}
if (($pendingRebootCheck -eq 'True') -and ($pendingRebootUptime -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'Error. You currently cannot have both PendingReboot features set to True. Please use them separately'
exit 1
}
if (($aDPasswordExpiration -eq 'True') -and ($pendingRebootCheck -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'Error. You cannot have both ADPasswordExpiration AND PendingRebootCheck set to True at the same time. Check your config'
exit 1
}
if (($aDPasswordExpiration -eq 'True') -and ($pendingRebootUptime -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'Error. You cannot have both ADPasswordExpiration AND PendingRebootUptime set to True at the same time. Check your config'
exit 1
}
if (($customAppEnabled -eq 'True') -and ($psAppStatus -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'Error. You cannot have both PowerShell app set to True AND CustomNotificationApp set to True at the same time. Check your config'
exit 1
}
if (($pendingRebootUptimeTextEnabled -eq 'True') -and ($pendingRebootCheckTextEnabled -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'Error. You cannot have PendingRebootUptimeText set to True and PendingRebootCheckText set to True at the same time'
Write-ToastLog -Level Error -Message 'You should only enable one of the text options. Check your config'
exit 1
}
if (($pendingRebootCheck -eq 'True') -and ($pendingRebootUptimeTextEnabled -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'Error. You cannot have PendingRebootCheck set to True and PendingRebootUptimeText set to True at the same time'
Write-ToastLog -Level Error -Message 'You should use PendingRebootCheck with the PendingRebootCheckText option instead'
exit 1
}
if (($pendingRebootUptime -eq 'True') -and ($pendingRebootCheckTextEnabled -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'Error. You cannot have PendingRebootUptime set to True and PendingRebootCheckText set to True at the same time'
Write-ToastLog -Level Error -Message 'You should use PendingRebootUptime with the PendingRebootUptimeText option instead. Check your config'
exit 1
}
if (($aDPasswordExpirationTextEnabled -eq 'True') -and ($pendingRebootCheckTextEnabled -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'Error. You cannot have ADPasswordExpirationTextEnabled set to True and PendingRebootCheckText set to True at the same time'
Write-ToastLog -Level Error -Message 'You should only enable one of the text options. Check your config'
exit 1
}
if (($aDPasswordExpirationTextEnabled -eq 'True') -and ($pendingRebootUptimeTextEnabled -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'Error. You cannot have ADPasswordExpirationTextEnabled set to True and PendingRebootUptimeTextEnabled set to True at the same time'
Write-ToastLog -Level Error -Message 'You should only enable one of the text options. Check your config'
exit 1
}
if (($deadlineEnabled -eq 'True') -and ($dynDeadlineEnabled -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'Error. You cannot have DeadlineEnabled set to True and DynamicDeadlineEnabled set to True at the same time'
Write-ToastLog -Level Error -Message 'You should only enable one of the deadline options. Check your config'
exit 1
}
if (($actionButton2Enabled -eq 'True') -and ($snoozeButtonEnabled -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'You cannot have ActionButton2 enabled and SnoozeButton enabled at the same time'
Write-ToastLog -Level Error -Message 'That will result in too many buttons. Check your config'
exit 1
}
if (($actionButton3Enabled -eq 'True') -and ($snoozeButtonEnabled -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'You cannot have ActionButton3 enabled and SnoozeButton enabled at the same time'
Write-ToastLog -Level Error -Message 'That will result in too many buttons. Check your config'
exit 1
}
if (($actionButton3Enabled -eq 'True') -and ($deadlineEnabled -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'You cannot have ActionButton3 enabled and Deadline enabled at the same time'
Write-ToastLog -Level Error -Message 'That will result in too many buttons. Check your config'
exit 1
}
if (($snoozeButtonEnabled -eq 'True') -and ($pendingRebootUptimeTextEnabled -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'You cannot have SnoozeButton enabled and have PendingRebootUptimeText enabled at the same time'
Write-ToastLog -Level Error -Message 'That will result in too much text and the toast notification will render without buttons. Check your config'
exit 1
}
if (($snoozeButtonEnabled -eq 'True') -and ($pendingRebootCheckTextEnabled -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'You cannot have SnoozeButton enabled and have PendingRebootCheckText enabled at the same time'
Write-ToastLog -Level Error -Message 'That will result in too much text and the toast notification will render without buttons. Check your config'
exit 1
}
if (($snoozeButtonEnabled -eq 'True') -and ($aDPasswordExpirationTextEnabled -eq 'True')) {
Write-ToastLog -Level Error -Message ('Error. Conflicting selection in the {0} file' -f $Config)
Write-ToastLog -Level Error -Message 'You cannot have SnoozeButton enabled and have ADPasswordExpirationText enabled at the same time'
Write-ToastLog -Level Error -Message 'That will result in too much text and the toast notification will render without buttons. Check your config'
exit 1
}
if ($action3 -match '^ToastRunPSScript:$' -and $action3 -notmatch '\.ps1') {
Write-ToastLog -Level Error -Message ('Error. Incomplete Value in the {0} file Action3 tag' -f $Config)
Write-ToastLog -Level Error -Message 'Error. You have to specify also the ps1 path: like ToastRunPSScript:C:\ProgramData\_Automation\Script\ScriptName.ps1'
exit 1
}
$psScriptPath = if ($action3 -match '^ToastRunPSScript:') {
(($action3 -split ':')[1..$($action3.Length)]) -join ':'
}
if ($action3 -match '^ToastRunPSScript:') {
if ([string]::IsNullOrEmpty($psScriptPath)) {
Write-ToastLog -Level Error -Message ('Error. Incomplete Value in the {0} file Action3 tag' -f $Config)
Write-ToastLog -Level Error -Message 'Error. You have to specify also the ps1 path: like ToastRunPSScript:C:\ProgramData\_Automation\Script\ScriptName.ps1'
exit 1
}
}
if ($psScriptPath) {
if (-not (Test-Path -Path $psScriptPath)) {
Write-ToastLog -Level Error -Message ('Provided path of the script to run ''{0}'' not found.' -f $psScriptPath)
exit 1
}
}
if ($action2 -match '^ToastLearnMore:$') {
Write-ToastLog -Level Error -Message ('Error. Incomplete Value in the {0} file Action2 tag' -f $Config)
Write-ToastLog -Level Error -Message 'Error. You have to specify also the learn more url: like ToastLearnMore:https:\\www.xyz.com'
exit 1
}
$learnMoreUrl = if ($action2 -match '^ToastLearnMore:') {