wingetautoupdate/Winget-AutoUpdate/functions/Start-NotifTask.ps1

66 lines
2.4 KiB
PowerShell
Raw Permalink Normal View History

2022-04-13 16:50:06 +00:00
#Function to send notifications to user
function Start-NotifTask ($Title, $Message, $MessageType, $Balise, $OnClickAction) {
2022-03-14 13:55:02 +00:00
2022-10-11 16:04:25 +00:00
if (($WAUConfig.WAU_NotificationLevel -eq "Full") -or ($WAUConfig.WAU_NotificationLevel -eq "SuccessOnly" -and $MessageType -eq "Success") -or ($UserRun)) {
2022-04-24 08:51:51 +00:00
#Prepare OnClickAction (if set)
2022-10-18 13:23:39 +00:00
if ($OnClickAction) {
$ToastOnClickAction = "activationType='protocol' launch='$OnClickAction'"
}
2022-04-24 08:51:51 +00:00
#Add XML variables
[xml]$ToastTemplate = @"
<toast $ToastOnClickAction>
2022-03-14 13:55:02 +00:00
<visual>
<binding template="ToastImageAndText03">
<text id="1">$Title</text>
<text id="2">$Message</text>
<image id="1" src="$WorkingDir\icons\$MessageType.png" />
</binding>
</visual>
<tag>$Balise</tag>
</toast>
"@
2022-04-24 08:51:51 +00:00
#if not "Interactive" user, run as system
if ($IsSystem) {
2022-04-24 08:51:51 +00:00
#Save XML to File
2022-10-23 21:36:15 +00:00
$ToastTemplateLocation = "$($WAUConfig.InstallLocation)\config\"
2022-06-10 08:26:41 +00:00
if (!(Test-Path $ToastTemplateLocation)) {
2022-04-24 08:51:51 +00:00
New-Item -ItemType Directory -Force -Path $ToastTemplateLocation
}
2022-05-22 13:31:17 +00:00
$ToastTemplate.Save("$ToastTemplateLocation\notif.xml")
2022-04-13 16:50:06 +00:00
2022-04-24 08:51:51 +00:00
#Run Notify scheduled task to notify conneted users
Get-ScheduledTask -TaskName "Winget-AutoUpdate-Notify" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue
2022-03-14 13:55:02 +00:00
}
2022-04-24 08:51:51 +00:00
#else, run as connected user
2022-06-10 08:26:41 +00:00
else {
2022-03-14 13:55:02 +00:00
2022-04-24 08:51:51 +00:00
#Load Assemblies
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
2022-04-13 16:50:06 +00:00
2022-04-24 08:51:51 +00:00
#Prepare XML
$ToastXml = [Windows.Data.Xml.Dom.XmlDocument]::New()
$ToastXml.LoadXml($ToastTemplate.OuterXml)
2022-03-14 13:55:02 +00:00
2022-04-24 08:51:51 +00:00
#Specify Launcher App ID
$LauncherID = "Windows.SystemToast.Winget.Notification"
#Prepare and Create Toast
$ToastMessage = [Windows.UI.Notifications.ToastNotification]::New($ToastXml)
$ToastMessage.Tag = $ToastTemplate.toast.tag
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($LauncherID).Show($ToastMessage)
2022-03-14 13:55:02 +00:00
2022-04-24 08:51:51 +00:00
}
2022-04-13 16:50:06 +00:00
2022-04-24 08:51:51 +00:00
#Wait for notification to display
Start-Sleep 3
2022-03-14 13:55:02 +00:00
2022-04-24 08:51:51 +00:00
}
2022-04-13 16:50:06 +00:00
}