From d8d71536e241d352d28ee56d107cdea62697484e Mon Sep 17 00:00:00 2001 From: romanitho <96626929+Romanitho@users.noreply.github.com> Date: Wed, 21 Jun 2023 15:47:38 +0200 Subject: [PATCH] Improved application detection after upgrade --- .../functions/Confirm-Installation.ps1 | 27 +++++++++ Winget-AutoUpdate/functions/Start-Init.ps1 | 3 + Winget-AutoUpdate/functions/Test-Network.ps1 | 3 - Winget-AutoUpdate/functions/Update-App.ps1 | 60 +++++++++---------- 4 files changed, 58 insertions(+), 35 deletions(-) create mode 100644 Winget-AutoUpdate/functions/Confirm-Installation.ps1 diff --git a/Winget-AutoUpdate/functions/Confirm-Installation.ps1 b/Winget-AutoUpdate/functions/Confirm-Installation.ps1 new file mode 100644 index 0000000..2f5fed2 --- /dev/null +++ b/Winget-AutoUpdate/functions/Confirm-Installation.ps1 @@ -0,0 +1,27 @@ +Function Confirm-Installation ($AppName, $AppVer){ + + #Set json export file + $JsonFile = "$WorkingDir\Config\InstalledApps.json" + + #Get installed apps and version in json file + & $Winget export -s winget -o $JsonFile --include-versions | Out-Null + + #Get json content + $Json = Get-Content $JsonFile -Raw | ConvertFrom-Json + + #Get apps and version in hashtable + $Packages = $Json.Sources.Packages + + #Remove json file + Remove-Item $JsonFile -Force + + # Search for specific app and version + $Apps = $Packages | Where-Object { $_.PackageIdentifier -eq $AppName -and $_.Version -eq $AppVer} + + if ($Apps){ + return $true + } + else{ + return $false + } +} \ No newline at end of file diff --git a/Winget-AutoUpdate/functions/Start-Init.ps1 b/Winget-AutoUpdate/functions/Start-Init.ps1 index ac5d5c5..b937ceb 100644 --- a/Winget-AutoUpdate/functions/Start-Init.ps1 +++ b/Winget-AutoUpdate/functions/Start-Init.ps1 @@ -5,6 +5,9 @@ function Start-Init { #Config console output encoding [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 + # Workaround for ARM64 (Access Denied / Win32 internal Server error) + $Script:ProgressPreference = 'SilentlyContinue' + $caller = Get-ChildItem $MyInvocation.PSCommandPath | Select-Object -Expand Name if ($caller -eq "Winget-Upgrade.ps1") { #Log Header diff --git a/Winget-AutoUpdate/functions/Test-Network.ps1 b/Winget-AutoUpdate/functions/Test-Network.ps1 index 5e4fc4f..9910d26 100644 --- a/Winget-AutoUpdate/functions/Test-Network.ps1 +++ b/Winget-AutoUpdate/functions/Test-Network.ps1 @@ -5,9 +5,6 @@ function Test-Network { #Init $timeout = 0 - # Workaround for ARM64 (Access Denied / Win32 internal Server error) - $ProgressPreference = 'SilentlyContinue' - #Test connectivity during 30 min then timeout Write-ToLog "Checking internet connection..." "Yellow" While ($timeout -lt 1800) { diff --git a/Winget-AutoUpdate/functions/Update-App.ps1 b/Winget-AutoUpdate/functions/Update-App.ps1 index 182ec57..7006b10 100644 --- a/Winget-AutoUpdate/functions/Update-App.ps1 +++ b/Winget-AutoUpdate/functions/Update-App.ps1 @@ -44,44 +44,40 @@ Function Update-App ($app) { } #Check if application updated properly - $CheckOutdated = Get-WingetOutdatedApps $FailedToUpgrade = $false - foreach ($CheckApp in $CheckOutdated) { - if ($($CheckApp.Id) -eq $($app.Id)) { + $ConfirmInstall = Confirm-Installation $($app.Id) $($app.AvailableVersion) - #Upgrade failed! - #Test for a Pending Reboot (Component Based Servicing/WindowsUpdate/CCM_ClientUtilities) - $PendingReboot = Test-PendingReboot - if ($PendingReboot -eq $true) { - Write-ToLog "-> A Pending Reboot lingers and probably prohibited $($app.Name) from upgrading...`n-> ...an install for $($app.Name) is NOT executed!" "Red" - $FailedToUpgrade = $true - break - } + if ($ConfirmInstall -ne $true) { + #Upgrade failed! + #Test for a Pending Reboot (Component Based Servicing/WindowsUpdate/CCM_ClientUtilities) + $PendingReboot = Test-PendingReboot + if ($PendingReboot -eq $true) { + Write-ToLog "-> A Pending Reboot lingers and probably prohibited $($app.Name) from upgrading...`n-> ...an install for $($app.Name) is NOT executed!" "Red" + $FailedToUpgrade = $true + break + } - #If app failed to upgrade, run Install command - Write-ToLog "-> An upgrade for $($app.Name) failed, now trying an install instead..." "Yellow" + #If app failed to upgrade, run Install command + Write-ToLog "-> An upgrade for $($app.Name) failed, now trying an install instead..." "Yellow" - if ($ModsOverride) { - Write-ToLog "-> Running (overriding default): Winget install --id $($app.Id) --accept-package-agreements --accept-source-agreements --force --override $ModsOverride" - & $Winget install --id $($app.Id) --accept-package-agreements --accept-source-agreements --force --override $ModsOverride | Tee-Object -file $LogFile -Append - } - else { - Write-ToLog "-> Running: Winget install --id $($app.Id) --accept-package-agreements --accept-source-agreements --force -h" - & $Winget install --id $($app.Id) --accept-package-agreements --accept-source-agreements --force -h | Tee-Object -file $LogFile -Append - } + if ($ModsOverride) { + Write-ToLog "-> Running (overriding default): Winget install --id $($app.Id) --accept-package-agreements --accept-source-agreements --force --override $ModsOverride" + & $Winget install --id $($app.Id) --accept-package-agreements --accept-source-agreements --force --override $ModsOverride | Tee-Object -file $LogFile -Append + } + else { + Write-ToLog "-> Running: Winget install --id $($app.Id) --accept-package-agreements --accept-source-agreements --force -h" + & $Winget install --id $($app.Id) --accept-package-agreements --accept-source-agreements --force -h | Tee-Object -file $LogFile -Append + } - if ($ModsInstall) { - Write-ToLog "Modifications for $($app.Id) during install are being applied..." "Yellow" - & "$ModsInstall" - } + if ($ModsInstall) { + Write-ToLog "Modifications for $($app.Id) during install are being applied..." "Yellow" + & "$ModsInstall" + } - #Check if application installed properly - $CheckOutdated2 = Get-WingetOutdatedApps - foreach ($CheckApp2 in $CheckOutdated2) { - if ($($CheckApp2.Id) -eq $($app.Id)) { - $FailedToUpgrade = $true - } - } + #Check if application installed properly + $ConfirmInstall = Confirm-Installation $($app.Id) $($app.AvailableVersion) + if ($ConfirmInstall -eq $false) { + $FailedToUpgrade = $true } }