From 9bccba7bcc7ee53eb2c12e634c0d65f9a76f1ef9 Mon Sep 17 00:00:00 2001 From: romanitho <96626929+Romanitho@users.noreply.github.com> Date: Mon, 31 Oct 2022 12:37:10 +0100 Subject: [PATCH 1/7] Improved user notification --- Winget-AutoUpdate-Install.ps1 | 9 ++- Winget-AutoUpdate/User-Run.ps1 | 75 +++++++++++++------ .../functions/Invoke-PostUpdateActions.ps1 | 14 ++++ .../functions/Start-NotifTask.ps1 | 2 +- 4 files changed, 74 insertions(+), 26 deletions(-) diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index 4173e0f..1a1c345 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -252,9 +252,16 @@ function Install-WingetAutoUpdate { } } - # Set dummy regkeys for notification name and icon + # Set regkeys for notification name, icon and actions & reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v DisplayName /t REG_EXPAND_SZ /d "Application Update" /f | Out-Null & reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v IconUri /t REG_EXPAND_SZ /d %SystemRoot%\system32\@WindowsUpdateToastIcon.png /f | Out-Null + $WAUClass = "HKLM:\Software\Classes\WAU" + $WAUClassRun = "Wscript.exe ""$WorkingDir\Invisible.vbs"" ""C:\Windows\System32\schtasks.exe /run /tn Winget-AutoUpdate""" + New-Item "HKLM:\Software\Classes\$($ActionType)\shell\open\command" -Force -ErrorAction SilentlyContinue | Out-Null + New-ItemProperty -LiteralPath $WAUClass -Name 'URL Protocol' -Value '' -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null + New-ItemProperty -LiteralPath $WAUClass -Name '(default)' -Value "URL:$($ActionType)" -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null + New-ItemProperty -LiteralPath $WAUClass -Name 'EditFlags' -Value '2162688' -PropertyType DWord -Force -ErrorAction SilentlyContinue | Out-Null + New-ItemProperty -LiteralPath "$WAUClass\shell\open\command" -Name '(default)' -Value $WAUClassRun -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null # Settings for the scheduled task for Updates $taskAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$($WingetUpdatePath)\winget-upgrade.ps1`"" diff --git a/Winget-AutoUpdate/User-Run.ps1 b/Winget-AutoUpdate/User-Run.ps1 index 5ec50a8..5b63878 100644 --- a/Winget-AutoUpdate/User-Run.ps1 +++ b/Winget-AutoUpdate/User-Run.ps1 @@ -29,24 +29,33 @@ function Test-WAUisRunning { } } -<# MAIN #> +<# FUNCTIONS #> #Get Working Dir $Script:WorkingDir = $PSScriptRoot -#Load external functions -. $WorkingDir\functions\Get-NotifLocale.ps1 -. $WorkingDir\functions\Start-NotifTask.ps1 +Get-ChildItem "$WorkingDir\functions" | ForEach-Object { . $_.FullName } + +function Test-WAUisRunning { + If (((Get-ScheduledTask -TaskName 'Winget-AutoUpdate').State -eq 'Running') -or ((Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext').State -eq 'Running')) { + Return $True + } +} + +<# MAIN #> + +#Run log initialisation function +Start-Init +Write-Log "User run initiated" #Get Toast Locale function -Get-NotifLocale +Get-NotifLocale | Out-Null -#Set common variables -$OnClickAction = "$WorkingDir\logs\updates.log" -$Button1Text = $NotifLocale.local.outputs.output[11].message -$Title = "Winget-AutoUpdate (WAU)" -$Balise = "Winget-AutoUpdate (WAU)" -$UserRun = $True +#Get WingetCmd function +Get-WingetCmd | Out-Null + +#Get WAU Configurations +$Script:WAUConfig = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" if ($Logs) { if ((Test-Path "$WorkingDir\logs\updates.log")) { @@ -68,22 +77,40 @@ else { if (Test-WAUisRunning) { $Message = $NotifLocale.local.outputs.output[8].message $MessageType = "warning" - Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss + $Button1Text = $NotifLocale.local.outputs.output[11].message + $Button1Action = "$WorkingDir\logs\updates.log" + Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $Button1Action -ButtonDismiss break } - #Run scheduled task - Get-ScheduledTask -TaskName "Winget-AutoUpdate" -ErrorAction Stop | Start-ScheduledTask -ErrorAction Stop - #Starting check - Send notification - $Message = $NotifLocale.local.outputs.output[6].message - $MessageType = "info" - Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss - #Sleep until the task is done - While (Test-WAUisRunning) { - Start-Sleep 3 + + #Get Outdated apps + $Outdated = Get-WingetOutdatedApps + $OutdatedApps = @() + #If White List + if ($WAUConfig.WAU_UseWhiteList -eq 1) { + $toUpdate = Get-IncludedApps + foreach ($app in $Outdated) { + if (($toUpdate -contains $app.Id) -and $($app.Version) -ne "Unknown") { + $OutdatedApps += $app.Name + } + } + } + #If Black List or default + else { + $toSkip = Get-ExcludedApps + foreach ($app in $Outdated) { + if (-not ($toSkip -contains $app.Id) -and $($app.Version) -ne "Unknown") { + $OutdatedApps += $app.Name + } + } + } + $body = $OutdatedApps | Out-String + if ($body) { + Start-NotifTask -Title "New available updates" -Message "Do you want to update these apps ?" -Body $body -ButtonDismiss -Button1Text "Yes" -Button1Action "wau:" -MessageType "info" + } + else { + Start-NotifTask -Title "All good." -Message "No new update available" -MessageType "success" } - $Message = $NotifLocale.local.outputs.output[9].message - $MessageType = "success" - Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss } catch { #Check failed - Just send notification diff --git a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 index 9d09782..0dd0616 100644 --- a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 +++ b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 @@ -89,6 +89,20 @@ function Invoke-PostUpdateActions { } } + #Add WAU Class to run if not exesting + $WAUClass = "HKLM:\Software\Classes\WAU" + if (!(Test-Path $WAUClass)) { + $WAUClassRun = "Wscript.exe ""$WorkingDir\Invisible.vbs"" ""C:\Windows\System32\schtasks.exe /run /tn Winget-AutoUpdate""" + New-Item "HKLM:\Software\Classes\$($ActionType)\shell\open\command" -Force -ErrorAction SilentlyContinue | Out-Null + New-ItemProperty -LiteralPath $WAUClass -Name 'URL Protocol' -Value '' -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null + New-ItemProperty -LiteralPath $WAUClass -Name '(default)' -Value "URL:$($ActionType)" -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null + New-ItemProperty -LiteralPath $WAUClass -Name 'EditFlags' -Value '2162688' -PropertyType DWord -Force -ErrorAction SilentlyContinue | Out-Null + New-ItemProperty -LiteralPath "$WAUClass\shell\open\command" -Name '(default)' -Value $WAUClassRun -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null + #log + Write-Log "-> WAU Notification Action declared." "green" + } + + #Reset WAU_UpdatePostActions Value $WAUConfig | New-ItemProperty -Name WAU_PostUpdateActions -Value 0 -Force diff --git a/Winget-AutoUpdate/functions/Start-NotifTask.ps1 b/Winget-AutoUpdate/functions/Start-NotifTask.ps1 index 668f387..990651f 100644 --- a/Winget-AutoUpdate/functions/Start-NotifTask.ps1 +++ b/Winget-AutoUpdate/functions/Start-NotifTask.ps1 @@ -14,7 +14,7 @@ function Start-NotifTask { [Switch]$ButtonDismiss = $false ) - if (($WAUConfig.WAU_NotificationLevel -eq "Full") -or ($WAUConfig.WAU_NotificationLevel -eq "SuccessOnly" -and $MessageType -eq "Success") -or ($UserRun)) { + if (($WAUConfig.WAU_NotificationLevel -eq "Full") -or ($WAUConfig.WAU_NotificationLevel -eq "SuccessOnly" -and $MessageType -eq "Success") -or (!$IsSystem)) { # XML Toast template creation [xml]$ToastTemplate = New-Object system.Xml.XmlDocument From 166993e567cc7e6965c8372f7369f465b1c02a67 Mon Sep 17 00:00:00 2001 From: romanitho <96626929+Romanitho@users.noreply.github.com> Date: Mon, 31 Oct 2022 12:52:44 +0100 Subject: [PATCH 2/7] small fix due to wrong copy/paste --- Winget-AutoUpdate-Install.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index 1a1c345..054bceb 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -256,7 +256,7 @@ function Install-WingetAutoUpdate { & reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v DisplayName /t REG_EXPAND_SZ /d "Application Update" /f | Out-Null & reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v IconUri /t REG_EXPAND_SZ /d %SystemRoot%\system32\@WindowsUpdateToastIcon.png /f | Out-Null $WAUClass = "HKLM:\Software\Classes\WAU" - $WAUClassRun = "Wscript.exe ""$WorkingDir\Invisible.vbs"" ""C:\Windows\System32\schtasks.exe /run /tn Winget-AutoUpdate""" + $WAUClassRun = "Wscript.exe ""$PSScriptRoot\Invisible.vbs"" ""C:\Windows\System32\schtasks.exe /run /tn Winget-AutoUpdate""" New-Item "HKLM:\Software\Classes\$($ActionType)\shell\open\command" -Force -ErrorAction SilentlyContinue | Out-Null New-ItemProperty -LiteralPath $WAUClass -Name 'URL Protocol' -Value '' -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null New-ItemProperty -LiteralPath $WAUClass -Name '(default)' -Value "URL:$($ActionType)" -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null From 59312812bb356b79006d2014f787b5fed06d3d56 Mon Sep 17 00:00:00 2001 From: romanitho <96626929+Romanitho@users.noreply.github.com> Date: Mon, 31 Oct 2022 12:58:25 +0100 Subject: [PATCH 3/7] small fix due to tiredness --- Winget-AutoUpdate-Install.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index 054bceb..2de52cf 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -256,7 +256,7 @@ function Install-WingetAutoUpdate { & reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v DisplayName /t REG_EXPAND_SZ /d "Application Update" /f | Out-Null & reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v IconUri /t REG_EXPAND_SZ /d %SystemRoot%\system32\@WindowsUpdateToastIcon.png /f | Out-Null $WAUClass = "HKLM:\Software\Classes\WAU" - $WAUClassRun = "Wscript.exe ""$PSScriptRoot\Invisible.vbs"" ""C:\Windows\System32\schtasks.exe /run /tn Winget-AutoUpdate""" + $WAUClassRun = "Wscript.exe ""$WingetUpdatePath\Invisible.vbs"" ""C:\Windows\System32\schtasks.exe /run /tn Winget-AutoUpdate""" New-Item "HKLM:\Software\Classes\$($ActionType)\shell\open\command" -Force -ErrorAction SilentlyContinue | Out-Null New-ItemProperty -LiteralPath $WAUClass -Name 'URL Protocol' -Value '' -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null New-ItemProperty -LiteralPath $WAUClass -Name '(default)' -Value "URL:$($ActionType)" -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null From a89f3782dbef73c6c851feb4b0ec322d84721d9f Mon Sep 17 00:00:00 2001 From: romanitho <96626929+Romanitho@users.noreply.github.com> Date: Mon, 31 Oct 2022 13:21:04 +0100 Subject: [PATCH 4/7] Uninstall part updated --- Winget-AutoUpdate-Install.ps1 | 8 +++++++- Winget-AutoUpdate/WAU-Uninstall.ps1 | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index 2de52cf..cff06dd 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -285,7 +285,12 @@ function Install-WingetAutoUpdate { $taskSettings = New-ScheduledTaskSettingsSet -Compatibility Win8 -StartWhenAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit 03:00:00 # Set up the task, and register it - $task = New-ScheduledTask -Action $taskAction -Principal $taskUserPrincipal -Settings $taskSettings -Trigger $taskTriggers + if ($taskTriggers) { + $task = New-ScheduledTask -Action $taskAction -Principal $taskUserPrincipal -Settings $taskSettings -Trigger $taskTriggers + } + else { + $task = New-ScheduledTask -Action $taskAction -Principal $taskUserPrincipal -Settings $taskSettings + } Register-ScheduledTask -TaskName 'Winget-AutoUpdate' -InputObject $task -Force | Out-Null if ($InstallUserContext) { @@ -413,6 +418,7 @@ function Uninstall-WingetAutoUpdate { Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$False & reg delete "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /f | Out-Null & reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" /f | Out-Null + & reg delete "HKLM\Software\Classes\WAU" /f | Out-Null if ((Test-Path "${env:ProgramData}\Microsoft\Windows\Start Menu\Programs\Winget-AutoUpdate (WAU)")) { Remove-Item -Path "${env:ProgramData}\Microsoft\Windows\Start Menu\Programs\Winget-AutoUpdate (WAU)" -Recurse -Force | Out-Null diff --git a/Winget-AutoUpdate/WAU-Uninstall.ps1 b/Winget-AutoUpdate/WAU-Uninstall.ps1 index 7590e47..a628626 100644 --- a/Winget-AutoUpdate/WAU-Uninstall.ps1 +++ b/Winget-AutoUpdate/WAU-Uninstall.ps1 @@ -52,6 +52,7 @@ try { Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$False & reg delete "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /f | Out-Null & reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" /f | Out-Null + & reg delete "HKLM\Software\Classes\WAU" /f | Out-Null if (Test-Path "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate") { & reg delete "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" /f | Out-Null } From 294949a4318f70c92084c946755a7765cdec8c3f Mon Sep 17 00:00:00 2001 From: romanitho <96626929+Romanitho@users.noreply.github.com> Date: Mon, 31 Oct 2022 13:32:59 +0100 Subject: [PATCH 5/7] Fixed wrong variable --- Winget-AutoUpdate-Install.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index cff06dd..4480275 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -257,7 +257,7 @@ function Install-WingetAutoUpdate { & reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v IconUri /t REG_EXPAND_SZ /d %SystemRoot%\system32\@WindowsUpdateToastIcon.png /f | Out-Null $WAUClass = "HKLM:\Software\Classes\WAU" $WAUClassRun = "Wscript.exe ""$WingetUpdatePath\Invisible.vbs"" ""C:\Windows\System32\schtasks.exe /run /tn Winget-AutoUpdate""" - New-Item "HKLM:\Software\Classes\$($ActionType)\shell\open\command" -Force -ErrorAction SilentlyContinue | Out-Null + New-Item $WAUClass -Force -ErrorAction SilentlyContinue | Out-Null New-ItemProperty -LiteralPath $WAUClass -Name 'URL Protocol' -Value '' -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null New-ItemProperty -LiteralPath $WAUClass -Name '(default)' -Value "URL:$($ActionType)" -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null New-ItemProperty -LiteralPath $WAUClass -Name 'EditFlags' -Value '2162688' -PropertyType DWord -Force -ErrorAction SilentlyContinue | Out-Null From c98ea467522c9348e95279b0bb3680850f04555b Mon Sep 17 00:00:00 2001 From: romanitho <96626929+Romanitho@users.noreply.github.com> Date: Mon, 31 Oct 2022 17:21:39 +0100 Subject: [PATCH 6/7] =?UTF-8?q?=F0=9F=98=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Winget-AutoUpdate-Install.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index 4480275..e25afa3 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -255,7 +255,7 @@ function Install-WingetAutoUpdate { # Set regkeys for notification name, icon and actions & reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v DisplayName /t REG_EXPAND_SZ /d "Application Update" /f | Out-Null & reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v IconUri /t REG_EXPAND_SZ /d %SystemRoot%\system32\@WindowsUpdateToastIcon.png /f | Out-Null - $WAUClass = "HKLM:\Software\Classes\WAU" + $WAUClass = "HKLM:\Software\Classes\WAU\shell\open\command" $WAUClassRun = "Wscript.exe ""$WingetUpdatePath\Invisible.vbs"" ""C:\Windows\System32\schtasks.exe /run /tn Winget-AutoUpdate""" New-Item $WAUClass -Force -ErrorAction SilentlyContinue | Out-Null New-ItemProperty -LiteralPath $WAUClass -Name 'URL Protocol' -Value '' -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null From fdd6171eeaa9058af48a796e4fe781b950fde1c7 Mon Sep 17 00:00:00 2001 From: romanitho <96626929+Romanitho@users.noreply.github.com> Date: Mon, 31 Oct 2022 17:25:37 +0100 Subject: [PATCH 7/7] =?UTF-8?q?=F0=9F=98=B5=F0=9F=94=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Winget-AutoUpdate-Install.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index e25afa3..ee65f41 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -255,9 +255,9 @@ function Install-WingetAutoUpdate { # Set regkeys for notification name, icon and actions & reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v DisplayName /t REG_EXPAND_SZ /d "Application Update" /f | Out-Null & reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v IconUri /t REG_EXPAND_SZ /d %SystemRoot%\system32\@WindowsUpdateToastIcon.png /f | Out-Null - $WAUClass = "HKLM:\Software\Classes\WAU\shell\open\command" + $WAUClass = "HKLM:\Software\Classes\WAU" $WAUClassRun = "Wscript.exe ""$WingetUpdatePath\Invisible.vbs"" ""C:\Windows\System32\schtasks.exe /run /tn Winget-AutoUpdate""" - New-Item $WAUClass -Force -ErrorAction SilentlyContinue | Out-Null + New-Item "$WAUClass\shell\open\command" -Force -ErrorAction SilentlyContinue | Out-Null New-ItemProperty -LiteralPath $WAUClass -Name 'URL Protocol' -Value '' -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null New-ItemProperty -LiteralPath $WAUClass -Name '(default)' -Value "URL:$($ActionType)" -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null New-ItemProperty -LiteralPath $WAUClass -Name 'EditFlags' -Value '2162688' -PropertyType DWord -Force -ErrorAction SilentlyContinue | Out-Null