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

69 lines
2.6 KiB
PowerShell
Raw 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-05-22 13:27:45 +00:00
if (($WAUConfig.WAU_NotificationLevel -eq "Full") -or ($WAUConfig.WAU_NotificationLevel -eq "SuccessOnly" -and $MessageType -eq "Success")) {
2022-04-24 08:51:51 +00:00
#Prepare OnClickAction (if set)
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
#Check if running account is system or interactive logon
$currentPrincipal = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-4")
#if not "Interactive" user, run as system
2022-06-10 08:26:41 +00:00
if ($currentPrincipal -eq $false) {
2022-04-24 08:51:51 +00:00
#Save XML to File
2022-05-22 13:31:17 +00:00
$ToastTemplateLocation = "$env:ProgramData\Winget-AutoUpdate\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
}