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] 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