From 2d4ed7c56596587181aec8967f5b47f0723448a9 Mon Sep 17 00:00:00 2001 From: Romain <96626929+Romanitho@users.noreply.github.com> Date: Sat, 19 Feb 2022 15:29:39 +0100 Subject: [PATCH] Update winget-upgrade.ps1 --- Winget-AutoUpdate/winget-upgrade.ps1 | 40 +++++++++++++++++++--------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/Winget-AutoUpdate/winget-upgrade.ps1 b/Winget-AutoUpdate/winget-upgrade.ps1 index 77725fb..d62b191 100644 --- a/Winget-AutoUpdate/winget-upgrade.ps1 +++ b/Winget-AutoUpdate/winget-upgrade.ps1 @@ -42,10 +42,10 @@ function Write-Log ($LogMsg,$LogColor = "White") { #Echo log $Log | Write-host -ForegroundColor $LogColor #Write log to file - $Log | out-file -filepath $LogFile -Append + $Log | Out-File -filepath $LogFile -Append } -function Start-NotifTask ($Title,$Message,$MessageType,$Balise) { +function Start-NotifTask ($Title,$Message,$MessageType,$Balise) { #Add XML variables [xml]$ToastTemplate = @" @@ -61,22 +61,36 @@ function Start-NotifTask ($Title,$Message,$MessageType,$Balise) { "@ - #Save XML File - $ToastTemplateLocation = "$env:ProgramData\Winget-AutoUpdate\" - if (!(Test-Path $ToastTemplateLocation)){ - New-Item -ItemType Directory -Force -Path $ToastTemplateLocation - } - $ToastTemplate.Save("$ToastTemplateLocation\notif.xml") + #Check if running account is system or interactive logon + $currentPrincipal = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-4") + if ($currentPrincipal -eq $false){ + #Save XML to File + $ToastTemplateLocation = "$env:ProgramData\Winget-AutoUpdate\" + if (!(Test-Path $ToastTemplateLocation)){ + New-Item -ItemType Directory -Force -Path $ToastTemplateLocation + } + $ToastTemplate.Save("$ToastTemplateLocation\notif.xml") - #Send Notification to user. First, check if script is run as admin (or system) - $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) - if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)){ #Run Notify scheduled task to notify conneted users Get-ScheduledTask -TaskName "Winget-AutoUpdate-Notify" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue } - #else, run as user + #else, run as connected user else{ - Start-Process powershell.exe -ArgumentList "-ExecutionPolicy Bypass -File `"$WorkingDir\winget-notify.ps1`"" -NoNewWindow -Wait + #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 + + #Prepare XML + $ToastXml = [Windows.Data.Xml.Dom.XmlDocument]::New() + $ToastXml.LoadXml($ToastTemplate.OuterXml) + + #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) } #Wait for notification to display