2023-09-15 14:33:51 +00:00
|
|
|
# Function to send the notifications to user
|
|
|
|
|
2023-09-15 14:38:54 +00:00
|
|
|
function Start-NotifTask
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
[CmdletBinding()]
|
2023-09-15 14:38:54 +00:00
|
|
|
param (
|
|
|
|
[String]
|
|
|
|
$Title = 'Winget-AutoUpdate',
|
|
|
|
[String]
|
|
|
|
$Message,
|
|
|
|
[String]
|
|
|
|
$MessageType,
|
|
|
|
[String]
|
|
|
|
$Balise = 'WAU',
|
|
|
|
[String]
|
|
|
|
$OnClickAction,
|
|
|
|
[String]
|
|
|
|
$Body,
|
|
|
|
[String]
|
|
|
|
$Button1Text,
|
|
|
|
[String]
|
|
|
|
$Button1Action,
|
|
|
|
[Switch]
|
|
|
|
$ButtonDismiss = $false,
|
|
|
|
[Switch]
|
|
|
|
$UserRun = $false
|
2023-09-15 14:33:51 +00:00
|
|
|
)
|
2023-09-15 14:38:54 +00:00
|
|
|
|
|
|
|
if (($WAUConfig.WAU_NotificationLevel -eq 'Full') -or ($WAUConfig.WAU_NotificationLevel -eq 'SuccessOnly' -and $MessageType -eq 'Success') -or ($UserRun))
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
# XML Toast template creation
|
|
|
|
[xml]$ToastTemplate = New-Object -TypeName system.Xml.XmlDocument
|
|
|
|
$ToastTemplate.LoadXml("<?xml version=`"1.0`" encoding=`"utf-8`"?><toast></toast>")
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33:51 +00:00
|
|
|
# Creation of visual node
|
|
|
|
$XMLvisual = $ToastTemplate.CreateElement('visual')
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33:51 +00:00
|
|
|
# Creation of a binding node
|
|
|
|
$XMLbinding = $ToastTemplate.CreateElement('binding')
|
|
|
|
$null = $XMLvisual.AppendChild($XMLbinding)
|
|
|
|
$XMLbindingAtt1 = ($ToastTemplate.CreateAttribute('template'))
|
|
|
|
$XMLbindingAtt1.Value = 'ToastGeneric'
|
|
|
|
$null = $XMLbinding.Attributes.Append($XMLbindingAtt1)
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33:51 +00:00
|
|
|
$XMLimagepath = ('{0}\icons\{1}.png' -f $WorkingDir, $MessageType)
|
2023-09-15 14:38:54 +00:00
|
|
|
if (Test-Path -Path $XMLimagepath -ErrorAction SilentlyContinue)
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
# Creation of a image node
|
|
|
|
$XMLimage = $ToastTemplate.CreateElement('image')
|
|
|
|
$null = $XMLbinding.AppendChild($XMLimage)
|
|
|
|
$XMLimageAtt1 = $ToastTemplate.CreateAttribute('placement')
|
|
|
|
$XMLimageAtt1.Value = 'appLogoOverride'
|
|
|
|
$null = $XMLimage.Attributes.Append($XMLimageAtt1)
|
|
|
|
$XMLimageAtt2 = $ToastTemplate.CreateAttribute('src')
|
|
|
|
$XMLimageAtt2.Value = ('{0}\icons\{1}.png' -f $WorkingDir, $MessageType)
|
|
|
|
$null = $XMLimage.Attributes.Append($XMLimageAtt2)
|
|
|
|
}
|
2023-09-15 14:38:54 +00:00
|
|
|
|
|
|
|
if ($Title)
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
# Creation of a text node
|
|
|
|
$XMLtitle = $ToastTemplate.CreateElement('text')
|
|
|
|
$XMLtitleText = $ToastTemplate.CreateTextNode($Title)
|
|
|
|
$null = $XMLtitle.AppendChild($XMLtitleText)
|
|
|
|
$null = $XMLbinding.AppendChild($XMLtitle)
|
|
|
|
}
|
2023-09-15 14:38:54 +00:00
|
|
|
|
|
|
|
if ($Message)
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
# Creation of a text node
|
|
|
|
$XMLtext = $ToastTemplate.CreateElement('text')
|
|
|
|
$XMLtextText = $ToastTemplate.CreateTextNode($Message)
|
|
|
|
$null = $XMLtext.AppendChild($XMLtextText)
|
|
|
|
$null = $XMLbinding.AppendChild($XMLtext)
|
|
|
|
}
|
2023-09-15 14:38:54 +00:00
|
|
|
|
|
|
|
if ($Body)
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
# Creation of a group node
|
|
|
|
$XMLgroup = $ToastTemplate.CreateElement('group')
|
|
|
|
$null = $XMLbinding.AppendChild($XMLgroup)
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33:51 +00:00
|
|
|
# Creation of a subgroup node
|
|
|
|
$XMLsubgroup = $ToastTemplate.CreateElement('subgroup')
|
|
|
|
$null = $XMLgroup.AppendChild($XMLsubgroup)
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33:51 +00:00
|
|
|
# Creation of a text node
|
|
|
|
$XMLcontent = $ToastTemplate.CreateElement('text')
|
|
|
|
$XMLcontentText = $ToastTemplate.CreateTextNode($Body)
|
|
|
|
$null = $XMLcontent.AppendChild($XMLcontentText)
|
|
|
|
$null = $XMLsubgroup.AppendChild($XMLcontent)
|
|
|
|
$XMLcontentAtt1 = $ToastTemplate.CreateAttribute('hint-style')
|
|
|
|
$XMLcontentAtt1.Value = 'body'
|
|
|
|
$null = $XMLcontent.Attributes.Append($XMLcontentAtt1)
|
|
|
|
$XMLcontentAtt2 = $ToastTemplate.CreateAttribute('hint-wrap')
|
|
|
|
$XMLcontentAtt2.Value = 'true'
|
|
|
|
$null = $XMLcontent.Attributes.Append($XMLcontentAtt2)
|
|
|
|
}
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33:51 +00:00
|
|
|
# Creation of actions node
|
|
|
|
$XMLactions = $ToastTemplate.CreateElement('actions')
|
2023-09-15 14:38:54 +00:00
|
|
|
|
|
|
|
if ($Button1Text)
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
# Creation of action node
|
|
|
|
$XMLaction = $ToastTemplate.CreateElement('action')
|
|
|
|
$null = $XMLactions.AppendChild($XMLaction)
|
|
|
|
$XMLactionAtt1 = $ToastTemplate.CreateAttribute('content')
|
|
|
|
$XMLactionAtt1.Value = $Button1Text
|
|
|
|
$null = $XMLaction.Attributes.Append($XMLactionAtt1)
|
2023-09-15 14:38:54 +00:00
|
|
|
if ($Button1Action)
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
$XMLactionAtt2 = $ToastTemplate.CreateAttribute('arguments')
|
|
|
|
$XMLactionAtt2.Value = $Button1Action
|
|
|
|
$null = $XMLaction.Attributes.Append($XMLactionAtt2)
|
|
|
|
$XMLactionAtt3 = $ToastTemplate.CreateAttribute('activationType')
|
|
|
|
$XMLactionAtt3.Value = 'Protocol'
|
|
|
|
$null = $XMLaction.Attributes.Append($XMLactionAtt3)
|
|
|
|
}
|
|
|
|
}
|
2023-09-15 14:38:54 +00:00
|
|
|
|
|
|
|
if ($ButtonDismiss)
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
# Creation of action node
|
|
|
|
$XMLaction = $ToastTemplate.CreateElement('action')
|
|
|
|
$null = $XMLactions.AppendChild($XMLaction)
|
|
|
|
$XMLactionAtt1 = $ToastTemplate.CreateAttribute('content')
|
|
|
|
$XMLactionAtt1.Value = ''
|
|
|
|
$null = $XMLaction.Attributes.Append($XMLactionAtt1)
|
|
|
|
$XMLactionAtt2 = $ToastTemplate.CreateAttribute('arguments')
|
|
|
|
$XMLactionAtt2.Value = 'dismiss'
|
|
|
|
$null = $XMLaction.Attributes.Append($XMLactionAtt2)
|
|
|
|
$XMLactionAtt3 = $ToastTemplate.CreateAttribute('activationType')
|
|
|
|
$XMLactionAtt3.Value = 'system'
|
|
|
|
$null = $XMLaction.Attributes.Append($XMLactionAtt3)
|
|
|
|
}
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33:51 +00:00
|
|
|
# Creation of tag node
|
|
|
|
$XMLtag = $ToastTemplate.CreateElement('tag')
|
|
|
|
$XMLtagText = $ToastTemplate.CreateTextNode($Balise)
|
|
|
|
$null = $XMLtag.AppendChild($XMLtagText)
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33:51 +00:00
|
|
|
# Add the visual node to the xml
|
|
|
|
$null = $ToastTemplate.LastChild.AppendChild($XMLvisual)
|
|
|
|
$null = $ToastTemplate.LastChild.AppendChild($XMLactions)
|
|
|
|
$null = $ToastTemplate.LastChild.AppendChild($XMLtag)
|
2023-09-15 14:38:54 +00:00
|
|
|
|
|
|
|
if ($OnClickAction)
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
$null = $ToastTemplate.toast.SetAttribute('activationType', 'Protocol')
|
|
|
|
$null = $ToastTemplate.toast.SetAttribute('launch', $OnClickAction)
|
|
|
|
}
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33:51 +00:00
|
|
|
# if not "Interactive" user, run as system
|
2023-09-15 14:38:54 +00:00
|
|
|
if ($IsSystem)
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
# Save XML to File
|
|
|
|
$ToastTemplateLocation = ('{0}\config\' -f $WAUConfig.InstallLocation)
|
2023-09-15 14:38:54 +00:00
|
|
|
if (!(Test-Path -Path $ToastTemplateLocation -ErrorAction SilentlyContinue))
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
$null = (New-Item -ItemType Directory -Force -Confirm:$false -Path $ToastTemplateLocation)
|
|
|
|
}
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33:51 +00:00
|
|
|
$ToastTemplate.Save(('{0}\notif.xml' -f $ToastTemplateLocation))
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33:51 +00:00
|
|
|
# Run Notify scheduled task to notify conneted users
|
|
|
|
$null = (Get-ScheduledTask -TaskName 'Winget-AutoUpdate-Notify' -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue)
|
|
|
|
}
|
2023-09-15 14:38:54 +00:00
|
|
|
else
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
#else, run as connected user
|
|
|
|
# Load Assemblies
|
|
|
|
$null = (Add-Type -AssemblyName Windows.UI)
|
|
|
|
$null = (Add-Type -AssemblyName Windows.Data)
|
|
|
|
$null = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
|
|
|
|
$null = [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33:51 +00:00
|
|
|
# Prepare XML
|
|
|
|
$ToastXml = [Windows.Data.Xml.Dom.XmlDocument]::New()
|
|
|
|
$ToastXml.LoadXml($ToastTemplate.OuterXml)
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33:51 +00:00
|
|
|
# Specify Launcher App ID
|
|
|
|
$LauncherID = 'Windows.SystemToast.Winget.Notification'
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33: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)
|
|
|
|
}
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33:51 +00:00
|
|
|
# Wait for notification to display
|
|
|
|
Start-Sleep -Seconds 3
|
|
|
|
}
|
2022-10-26 22:49:10 +00:00
|
|
|
}
|