-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_notification.ps1
More file actions
31 lines (26 loc) · 1.42 KB
/
send_notification.ps1
File metadata and controls
31 lines (26 loc) · 1.42 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
param (
[string]$Title = "SortMeDown",
[string]$Message = "A file has been moved."
)
try {
# Load required Windows Runtime assemblies
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom, ContentType = WindowsRuntime] | Out-Null
# Get the toast notification template
$template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)
# Set the title and message text
$xml = $template.GetElementsByTagName("text")
$xml[0].AppendChild($template.CreateTextNode($Title)) | Out-Null
$xml[1].AppendChild($template.CreateTextNode($Message)) | Out-Null
# Create the toast notification from the XML
$toast = [Windows.UI.Notifications.ToastNotification]::new($template)
# Show the notification
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("SortMeDown")
$notifier.Show($toast)
}
catch {
# Write any errors to a log file in the temp directory to avoid polluting the main console
$errorLogPath = Join-Path $env:TEMP "SortMeDown_Notification_Error.log"
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"[$timestamp] Error sending notification: $($_.Exception.Message)" | Out-File -FilePath $errorLogPath -Append
}