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

176 lines
7.3 KiB
PowerShell
Raw Normal View History

2023-03-31 15:56:07 +00:00
#Function to send the notifications to user
2022-04-13 16:50:06 +00:00
2022-10-25 08:43:02 +00:00
function Start-NotifTask {
param(
[String]$Title = "Winget-AutoUpdate",
[String]$Message,
[String]$MessageType,
[String]$Balise = "WAU",
[String]$OnClickAction,
[String]$Body,
[String]$Button1Text,
[String]$Button1Action,
2023-04-10 19:10:15 +00:00
[Switch]$ButtonDismiss = $false,
[Switch]$UserRun = $false
2022-10-25 08:43:02 +00:00
)
2022-03-14 13:55:02 +00:00
2023-04-24 22:43:04 +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
2022-10-25 08:43:02 +00:00
# XML Toast template creation
[xml]$ToastTemplate = New-Object system.Xml.XmlDocument
$ToastTemplate.LoadXml("<?xml version=`"1.0`" encoding=`"utf-8`"?><toast></toast>")
# Creation of visual node
$XMLvisual = $ToastTemplate.CreateElement("visual")
# Creation of a binding node
$XMLbinding = $ToastTemplate.CreateElement("binding")
$XMLvisual.AppendChild($XMLbinding) | Out-Null
$XMLbindingAtt1 = ($ToastTemplate.CreateAttribute("template"))
$XMLbindingAtt1.Value = "ToastGeneric"
$XMLbinding.Attributes.Append($XMLbindingAtt1) | Out-Null
$XMLimagepath = "$WorkingDir\icons\$MessageType.png"
2023-03-31 15:56:07 +00:00
if (Test-Path $XMLimagepath) {
2022-10-25 08:43:02 +00:00
# Creation of a image node
$XMLimage = $ToastTemplate.CreateElement("image")
$XMLbinding.AppendChild($XMLimage) | Out-Null
$XMLimageAtt1 = $ToastTemplate.CreateAttribute("placement")
$XMLimageAtt1.Value = "appLogoOverride"
$XMLimage.Attributes.Append($XMLimageAtt1) | Out-Null
$XMLimageAtt2 = $ToastTemplate.CreateAttribute("src")
$XMLimageAtt2.Value = "$WorkingDir\icons\$MessageType.png"
$XMLimage.Attributes.Append($XMLimageAtt2) | Out-Null
}
2023-03-31 15:56:07 +00:00
if ($Title) {
2022-10-25 08:43:02 +00:00
# Creation of a text node
$XMLtitle = $ToastTemplate.CreateElement("text")
$XMLtitleText = $ToastTemplate.CreateTextNode($Title)
$XMLtitle.AppendChild($XMLtitleText) | Out-Null
$XMLbinding.AppendChild($XMLtitle) | Out-Null
}
2023-03-31 15:56:07 +00:00
if ($Message) {
2022-10-25 08:43:02 +00:00
# Creation of a text node
$XMLtext = $ToastTemplate.CreateElement("text")
$XMLtextText = $ToastTemplate.CreateTextNode($Message)
$XMLtext.AppendChild($XMLtextText) | Out-Null
$XMLbinding.AppendChild($XMLtext) | Out-Null
}
2023-03-31 15:56:07 +00:00
if ($Body) {
2022-10-25 08:43:02 +00:00
# Creation of a group node
$XMLgroup = $ToastTemplate.CreateElement("group")
$XMLbinding.AppendChild($XMLgroup) | Out-Null
# Creation of a subgroup node
$XMLsubgroup = $ToastTemplate.CreateElement("subgroup")
$XMLgroup.AppendChild($XMLsubgroup) | Out-Null
# Creation of a text node
$XMLcontent = $ToastTemplate.CreateElement("text")
$XMLcontentText = $ToastTemplate.CreateTextNode($Body)
$XMLcontent.AppendChild($XMLcontentText) | Out-Null
$XMLsubgroup.AppendChild($XMLcontent) | Out-Null
$XMLcontentAtt1 = $ToastTemplate.CreateAttribute("hint-style")
$XMLcontentAtt1.Value = "body"
$XMLcontent.Attributes.Append($XMLcontentAtt1) | Out-Null
$XMLcontentAtt2 = $ToastTemplate.CreateAttribute("hint-wrap")
$XMLcontentAtt2.Value = "true"
$XMLcontent.Attributes.Append($XMLcontentAtt2) | Out-Null
}
# Creation of actions node
$XMLactions = $ToastTemplate.CreateElement("actions")
if ($Button1Text) {
# Creation of action node
$XMLaction = $ToastTemplate.CreateElement("action")
$XMLactions.AppendChild($XMLaction) | Out-Null
$XMLactionAtt1 = $ToastTemplate.CreateAttribute("content")
$XMLactionAtt1.Value = $Button1Text
$XMLaction.Attributes.Append($XMLactionAtt1) | Out-Null
2023-03-31 15:56:07 +00:00
if ($Button1Action) {
2022-10-25 08:43:02 +00:00
$XMLactionAtt2 = $ToastTemplate.CreateAttribute("arguments")
$XMLactionAtt2.Value = $Button1Action
$XMLaction.Attributes.Append($XMLactionAtt2) | Out-Null
$XMLactionAtt3 = $ToastTemplate.CreateAttribute("activationType")
$XMLactionAtt3.Value = "Protocol"
$XMLaction.Attributes.Append($XMLactionAtt3) | Out-Null
}
}
if ($ButtonDismiss) {
# Creation of action node
$XMLaction = $ToastTemplate.CreateElement("action")
$XMLactions.AppendChild($XMLaction) | Out-Null
$XMLactionAtt1 = $ToastTemplate.CreateAttribute("content")
$XMLactionAtt1.Value = ""
$XMLaction.Attributes.Append($XMLactionAtt1) | Out-Null
$XMLactionAtt2 = $ToastTemplate.CreateAttribute("arguments")
$XMLactionAtt2.Value = "dismiss"
$XMLaction.Attributes.Append($XMLactionAtt2) | Out-Null
$XMLactionAtt3 = $ToastTemplate.CreateAttribute("activationType")
$XMLactionAtt3.Value = "system"
$XMLaction.Attributes.Append($XMLactionAtt3) | Out-Null
}
# Creation of tag node
$XMLtag = $ToastTemplate.CreateElement("tag")
$XMLtagText = $ToastTemplate.CreateTextNode($Balise)
$XMLtag.AppendChild($XMLtagText) | Out-Null
# Add the visual node to the xml
$ToastTemplate.LastChild.AppendChild($XMLvisual) | Out-Null
$ToastTemplate.LastChild.AppendChild($XMLactions) | Out-Null
$ToastTemplate.LastChild.AppendChild($XMLtag) | Out-Null
2023-03-31 15:56:07 +00:00
if ($OnClickAction) {
2022-10-25 08:43:02 +00:00
$ToastTemplate.toast.SetAttribute("activationType", "Protocol") | Out-Null
$ToastTemplate.toast.SetAttribute("launch", $OnClickAction) | Out-Null
}
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-10-26 22:49:10 +00:00
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"
2022-10-26 22:49:10 +00:00
2022-04-24 08:51:51 +00:00
#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
2022-10-26 22:49:10 +00:00
}