From 25e02c23d972bd7aad8c0ee0fc39a385413a084e Mon Sep 17 00:00:00 2001 From: Romain <96626929+Romanitho@users.noreply.github.com> Date: Wed, 10 May 2023 17:51:18 +0200 Subject: [PATCH] better way --- Winget-AutoUpdate/User-Run.ps1 | 2 +- Winget-AutoUpdate/Winget-Upgrade.ps1 | 42 +++++------ .../functions/Invoke-UserApproval.ps1 | 71 +++++++------------ 3 files changed, 45 insertions(+), 70 deletions(-) diff --git a/Winget-AutoUpdate/User-Run.ps1 b/Winget-AutoUpdate/User-Run.ps1 index 35917ec..1631a22 100644 --- a/Winget-AutoUpdate/User-Run.ps1 +++ b/Winget-AutoUpdate/User-Run.ps1 @@ -62,7 +62,7 @@ elseif ($Help) { } elseif ($NotifApproved){ $MessageBody = "Do you want to update these apps ?`n`n" - $MessageBody += Get-Content "$WorkingDir/config/NotifContent.txt" -Raw + $MessageBody += (Import-Csv "$WorkingDir/Config/NotifAppList.csv").name $Title = "Winget-AutoUpdate" if ($NotifApproved -eq "wau:systemDialogBox"){ Add-Type -AssemblyName PresentationCore,PresentationFramework diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index 667bb3c..75477f3 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -236,29 +236,6 @@ if (Test-Network) { $Log | out-file -filepath $LogFile -Append } - #Ask user to approve, if configured - if ($WAUConfig.WAU_UserApproval -eq 1){ - Write-ToLog "User Approval feature enabled." - - #Check for approved tag - $WAUNotifApproved = "$WorkingDir/Config/NotifApproved.txt" - if (Test-Path $WAUNotifApproved) { - Write-ToLog "-> User approved update notification." - Remove-Item $WAUNotifApproved -Force -Confirm:$false - } - else { - $UserApprovalReturn = Invoke-UserApproval $outdated - if ($UserApprovalReturn -eq 0){ - Write-ToLog "-> User approval requested. Waiting for user to approve available updates... Closing for now." - #Closing job, waiting for user approval - Exit 0 - } - else{ - Write-ToLog "-> No update to request to user." - } - } - } - #Count good update installations $Script:InstallOK = 0 @@ -269,12 +246,14 @@ if (Test-Network) { $toSkip = $null } + #Generate App List to update + $Script:AppListToUpdate = @() #If White List if ($UseWhiteList) { #For each app, notify and update foreach ($app in $outdated) { if (($toUpdate -contains $app.Id) -and $($app.Version) -ne "Unknown") { - Update-App $app + $AppListToUpdate += $app } #if current app version is unknown elseif ($($app.Version) -eq "Unknown") { @@ -291,7 +270,7 @@ if (Test-Network) { #For each app, notify and update foreach ($app in $outdated) { if (-not ($toSkip -contains $app.Id) -and $($app.Version) -ne "Unknown") { - Update-App $app + $AppListToUpdate += $app } #if current app version is unknown elseif ($($app.Version) -eq "Unknown") { @@ -304,6 +283,19 @@ if (Test-Network) { } } + #Ask user to approve, if configured + if ($WAUConfig.WAU_UserApproval -eq 1){ + Write-ToLog "User Approval feature enabled." + if ($AppListToUpdate){ + Invoke-UserApproval $AppListToUpdate + } + } + + #Update apps + foreach ($App in $AppListToUpdate){ + Update-App $App + } + if ($InstallOK -gt 0) { Write-ToLog "$InstallOK apps updated ! No more update." "Green" } diff --git a/Winget-AutoUpdate/functions/Invoke-UserApproval.ps1 b/Winget-AutoUpdate/functions/Invoke-UserApproval.ps1 index 79ab72a..007fb96 100644 --- a/Winget-AutoUpdate/functions/Invoke-UserApproval.ps1 +++ b/Winget-AutoUpdate/functions/Invoke-UserApproval.ps1 @@ -1,58 +1,41 @@ #Function to ask user consent before updating apps -function Invoke-UserApproval ($outdated){ +function Invoke-UserApproval ($Apps){ - #Create / Update WAU Class for notification action - if ($IsSystem) { - $WAUClass = "HKLM:\Software\Classes\WAU" - $WAUClassCmd = "$WAUClass\shell\open\command" - $WAUClassRun = "Wscript.exe ""$WorkingDir\Invisible.vbs"" ""powershell.exe -NoProfile -ExecutionPolicy Bypass -Command & '$WorkingDir\User-Run.ps1' -NotifApproved %1""" - New-Item $WAUClassCmd -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:WAU" -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 $WAUClassCmd -Name '(default)' -Value $WAUClassRun -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null - $Button1Action = "wau:system" - $OnClickAction = "wau:systemDialogBox" - } - else{ - $Button1Action = "wau:user" - $OnClickAction = "wau:userDialogBox" - } + $WAUNotifApproved = "$WorkingDir/Config/NotifApproved.txt" + $WAUNotifAppList = "$WorkingDir/Config/NotifAppList.csv" - $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)" - } - } + #Check for approved file + if (Test-Path $WAUNotifApproved) { + Write-ToLog "-> User approved update notification." + $AppListToUpdate = Import-Csv $WAUNotifAppList + Remove-Item $WAUNotifApproved -Force -Confirm:$false + Remove-Item $WAUNotifAppList -Force -Confirm:$false } - #If Black List or default + #Otherwise generate AppList and send notif else { - $toSkip = Get-ExcludedApps - foreach ($app in $Outdated) { - if (-not ($toSkip -contains $app.Id) -and $($app.Version) -ne "Unknown") { - $OutdatedApps += "- $($app.Name)" - } - } - } + Write-ToLog "-> Creating AppList user must approve" + $Apps | Export-Csv -Path $WAUNotifAppList -NoTypeInformation -Encoding UTF8 + + if ($IsSystem) { + $Button1Action = "wau:system" + $OnClickAction = "wau:systemDialogBox" + } + else{ + $Button1Action = "wau:user" + $OnClickAction = "wau:userDialogBox" + } - $body = $OutdatedApps | Out-String - if ($body) { #Ask user to update apps + + $body = $Apps.Name | Out-String $Message = "Do you want to update these apps ?" $body += "`nPlease save your work and close theses apps" - $WAUNotifContent = "$WorkingDir\config\NotifContent.txt" - New-Item -Path $WAUNotifContent -ItemType File -Force | Out-Null - Set-Content -Path $WAUNotifContent -Value $body Start-NotifTask -Title "New available updates" -Message $Message -Body $body -ButtonDismiss -Button1Text "Yes" -Button1Action $Button1Action -OnClickAction $OnClickAction -MessageType "info" - Return 0 - } - else { - Return 1 + + Write-ToLog "-> User approval requested. Waiting for user to approve available updates... Closing for now." + #Closing job, waiting for user approval + Exit 0 } } \ No newline at end of file