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 1/3] 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 From d5a8ad28739ecab193d8456f987900b8c4e1abfd Mon Sep 17 00:00:00 2001 From: Romain <96626929+Romanitho@users.noreply.github.com> Date: Sat, 19 Feb 2022 16:08:39 +0100 Subject: [PATCH 2/3] Put log in user profile if run with user context --- Winget-AutoUpdate/winget-upgrade.ps1 | 29 ++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/Winget-AutoUpdate/winget-upgrade.ps1 b/Winget-AutoUpdate/winget-upgrade.ps1 index d62b191..d6e991d 100644 --- a/Winget-AutoUpdate/winget-upgrade.ps1 +++ b/Winget-AutoUpdate/winget-upgrade.ps1 @@ -4,19 +4,28 @@ function Init { #Var $Script:WorkingDir = $PSScriptRoot - #Logs initialisation - $LogPath = "$WorkingDir\logs" - if (!(Test-Path $LogPath)){ - New-Item -ItemType Directory -Force -Path $LogPath - } - - #Log file - $Script:LogFile = "$LogPath\updates.log" - #Log Header $Log = "##################################################`n# CHECK FOR APP UPDATES - $(Get-Date -Format 'dd/MM/yyyy')`n##################################################" $Log | Write-host - $Log | out-file -filepath $LogFile -Append + try{ + #Logs initialisation + $LogPath = "$WorkingDir\logs" + if (!(Test-Path $LogPath)){ + New-Item -ItemType Directory -Force -Path $LogPath + } + #Log file + $Script:LogFile = "$LogPath\updates.log" + $Log | out-file -filepath $LogFile -Append + } + catch{ + #Logs initialisation + $LogPath = "$env:USERPROFILE\Winget-AutoUpdate\logs" + if (!(Test-Path $LogPath)){ + New-Item -ItemType Directory -Force -Path $LogPath + } + $Script:LogFile = "$LogPath\updates.log" + $Log | out-file -filepath $LogFile -Append + } #Get locale file for Notification #Default en-US From 91279e1753b1cc2c34dba529470d9527bf29a314 Mon Sep 17 00:00:00 2001 From: Romain <96626929+Romanitho@users.noreply.github.com> Date: Sun, 20 Feb 2022 10:40:20 +0100 Subject: [PATCH 3/3] Update winget-upgrade.ps1 --- Winget-AutoUpdate/winget-upgrade.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/Winget-AutoUpdate/winget-upgrade.ps1 b/Winget-AutoUpdate/winget-upgrade.ps1 index d6e991d..baf167d 100644 --- a/Winget-AutoUpdate/winget-upgrade.ps1 +++ b/Winget-AutoUpdate/winget-upgrade.ps1 @@ -72,6 +72,7 @@ function Start-NotifTask ($Title,$Message,$MessageType,$Balise) { #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 if ($currentPrincipal -eq $false){ #Save XML to File $ToastTemplateLocation = "$env:ProgramData\Winget-AutoUpdate\"