@@ -16,6 +16,9 @@ To uninstall app. Works with AppIDs
1616. PARAMETER AllowUpgrade
1717To allow upgrade app if present. Works with AppIDs
1818
19+ . PARAMETER DisableRetry
20+ Disables the retry logic for installation and uninstallation on errors. Only one (un)install attempt will be made.
21+
1922. PARAMETER LogPath
2023Used to specify logpath. Default is same folder as Winget-Autoupdate project
2124
@@ -48,9 +51,10 @@ param(
4851 [Parameter (Mandatory = $False )] [Switch ] $Uninstall ,
4952 [Parameter (Mandatory = $False )] [String ] $LogPath ,
5053 [Parameter (Mandatory = $False )] [Switch ] $WAUWhiteList ,
51- [Parameter (Mandatory = $False )] [Switch ] $AllowUpgrade
54+ [Parameter (Mandatory = $False )] [Switch ] $AllowUpgrade ,
55+ [Parameter (Mandatory = $False )] [Switch ] $DisableRetry
5256)
53-
57+ $ Script :ExitCode = 0
5458
5559<# FUNCTIONS #>
5660
@@ -59,7 +63,8 @@ $scriptItem = Get-Item -LiteralPath $MyInvocation.MyCommand.Definition
5963$realPath = if ($scriptItem.LinkType ) {
6064 $targetPath = [System.IO.Path ]::GetFullPath([System.IO.Path ]::Combine($scriptItem.Directory.FullName , $scriptItem.Target ))
6165 Split-Path - Parent $targetPath
62- } else {
66+ }
67+ else {
6368 $scriptItem.DirectoryName
6469}
6570
@@ -74,15 +79,21 @@ $realPath = if ($scriptItem.LinkType) {
7479# Check if App exists in Winget Repository
7580function Confirm-Exist ($AppID ) {
7681 # Check is app exists in the winget repository
77- $WingetApp = & $winget show -- Id $AppID - e -- accept- source- agreements - s winget | Out-String
82+ try {
83+ $WingetApp = & $winget show -- Id $AppID - e -- accept- source- agreements - s winget | Out-String
7884
79- # Return if AppID exists
80- if ($WingetApp -match [regex ]::Escape($AppID )) {
81- Write-ToLog " -> $AppID exists on Winget Repository." " Cyan"
82- return $true
85+ # Return if AppID exists
86+ if ($WingetApp -match [regex ]::Escape($AppID )) {
87+ Write-ToLog " -> $AppID exists on Winget Repository." " Cyan"
88+ return $true
89+ }
90+ else {
91+ Write-ToLog " -> $AppID does not exist on Winget Repository! Check spelling." " Red"
92+ return $false
93+ }
8394 }
84- else {
85- Write-ToLog " -> $AppID does not exist on Winget Repository! Check spelling. " " Red"
95+ catch {
96+ Write-ToLog " Error $_ while checking winget package availability " " Red"
8697 return $false
8798 }
8899}
@@ -147,6 +158,7 @@ function Test-ModsUninstall ($AppID) {
147158 return $ModsPreUninstall , $ModsUninstall , $ModsUninstalled
148159}
149160
161+ # Install function
150162# Install function
151163function Install-App ($AppID , $AppArgs ) {
152164 $IsInstalled = Confirm-Installation $AppID
@@ -160,21 +172,55 @@ function Install-App ($AppID, $AppArgs) {
160172 & " $ModsPreInstall "
161173 }
162174
163- # Install App
164- Write-ToLog " -> Installing $AppID ..." " Yellow"
165- $WingetArgs = " install --id $AppID -e --accept-package-agreements --accept-source-agreements -s winget -h $AppArgs " -split " "
166- Write-ToLog " -> Running: `" $Winget `" $WingetArgs "
167- & " $Winget " $WingetArgs | Where-Object { $_ -notlike " *" } | Tee-Object - file $LogFile - Append
175+ # Install App with retry logic
176+ $retryCount = 0
177+ if ($DisableRetry ) {
178+ $maxRetries = 0
179+ }
180+ else {
181+ $maxRetries = 2
182+ }
183+ $installSuccess = $false
184+
185+ while ($retryCount -le $maxRetries -and -not $installSuccess ) {
186+ Write-ToLog " -> Installing $AppID (Attempt: $ ( $retryCount ) )..." " Yellow"
187+ $retryCount ++
188+ $WingetArgs = " install --id $AppID -e --accept-package-agreements --accept-source-agreements -s winget -h $AppArgs " -split " "
189+ Write-ToLog " -> Running: `" $Winget `" $WingetArgs "
190+ & " $Winget " $WingetArgs | Where-Object { $_ -notlike " *" } | Tee-Object - file $LogFile - Append
191+ if (-not ([String ]::IsNullOrEmpty($LASTEXITCODE ))) {
192+ $Script :ExitCode = $LASTEXITCODE
193+ }
194+ else {
195+ $Script :ExitCode = 0
196+ }
168197
169- if ($ModsInstall ) {
170- Write-ToLog " -> Modifications for $AppID during install are being applied..." " Yellow"
171- & " $ModsInstall "
198+ if ($exitCode -eq 0 ) {
199+ $installSuccess = $true
200+ Write-ToLog " -> $AppID successfully installed." " Green"
201+ }
202+ else {
203+ if ($retryCount -lt $maxRetries -and -not $DisableRetry ) {
204+ $RetryText = " Retrying... (Retry $retryCount of $maxRetries )"
205+ }
206+ else {
207+ $RetryText = " "
208+ }
209+ Write-ToLog " -> $AppID installation failed with Exit Code: $exitCode .$RetryText " " Red"
210+ Start-Sleep 5
211+ }
172212 }
173213
174- # Check if install is ok
175- $IsInstalled = Confirm-Installation $AppID
176- if ($IsInstalled ) {
177- Write-ToLog " -> $AppID successfully installed." " Green"
214+ if (-not $installSuccess ) {
215+ Write-ToLog " -> $AppID installation failed after $ ( $maxRetries + 1 ) attempts!" " Red"
216+ }
217+
218+ # Apply post-installation modifications if the installation was successful
219+ if ($installSuccess ) {
220+ if ($ModsInstall ) {
221+ Write-ToLog " -> Modifications for $AppID during install are being applied..." " Yellow"
222+ & " $ModsInstall "
223+ }
178224
179225 if ($ModsInstalledOnce ) {
180226 Write-ToLog " -> Modifications for $AppID after install (one time) are being applied..." " Yellow"
@@ -201,66 +247,84 @@ function Install-App ($AppID, $AppArgs) {
201247 Add-WAUWhiteList $AppID
202248 }
203249 }
204- else {
205- Write-ToLog " -> $AppID installation failed!" " Red"
206- }
207250 }
208251 else {
209252 Write-ToLog " -> $AppID is already installed." " Cyan"
210253 }
211254}
212255
213- # Uninstall function
214- function Uninstall-App ($AppID , $AppArgs ) {
256+ # Uninstall function
257+ function Uninstall-App ($AppID ) {
215258 $IsInstalled = Confirm-Installation $AppID
216259 if ($IsInstalled ) {
217- # Check if mods exist (or already exist) for preuninstall/uninstall/uninstalled
218- $ModsPreUninstall , $ModsUninstall , $ModsUninstalled = Test-ModsUninstall $AppID
260+ # Check if mods exist for uninstalling
261+ $ModsPreUninstall , $ModsUninstall , $ModsUninstalled = Test-ModsUninstall $ ( $ AppID)
219262
220- # If PreUninstall script exist
263+ # If PreUninstall script exists
221264 if ($ModsPreUninstall ) {
222265 Write-ToLog " -> Modifications for $AppID before uninstall are being applied..." " Yellow"
223266 & " $ModsPreUninstall "
224267 }
225268
226- # Uninstall App
227- Write-ToLog " -> Uninstalling $AppID ..." " Yellow"
228- $WingetArgs = " uninstall --id $AppID -e --accept-source-agreements -h" -split " "
229- Write-ToLog " -> Running: `" $Winget `" $WingetArgs "
230- & " $Winget " $WingetArgs | Where-Object { $_ -notlike " *" } | Tee-Object - file $LogFile - Append
269+ # Stop running processes related to the application before uninstalling
270+ Write-ToLog " -> Stopping processes related to $AppID ..." " Yellow"
271+ Stop-Process - Name $AppID - Force - ErrorAction SilentlyContinue
231272
232- if ($ModsUninstall ) {
233- Write-ToLog " -> Modifications for $AppID during uninstall are being applied..." " Yellow"
234- & " $ModsUninstall "
273+ # Uninstall with retry logic
274+ $retryCount = 0
275+ if ($DisableRetry ) {
276+ $maxRetries = 0
235277 }
278+ else {
279+ $maxRetries = 2
280+ }
281+ $uninstallSuccess = $false
282+
283+ while ($retryCount -le $maxRetries -and -not $uninstallSuccess ) {
284+ Write-ToLog " -> Uninstalling $AppID (Attempt: $ ( $retryCount ) )..." " Yellow"
285+ $retryCount ++
286+ $WingetArgs = " uninstall --id $AppID -e --accept-source-agreements -s winget" -split " "
287+ Write-ToLog " -> Running: \" $Winget \" $WingetArgs "
288+ & " $Winget " $WingetArgs | Where-Object { $_ -notlike " *" } | Tee-Object - file $LogFile - Append
289+ if (-not ([String ]::IsNullOrEmpty($LASTEXITCODE ))) {
290+ $Script :ExitCode = $LASTEXITCODE
291+ }
292+ else {
293+ $Script :ExitCode = 0
294+ }
295+
296+ if ($exitCode -eq 0 ) {
297+ $uninstallSuccess = $true
298+ Write-ToLog " -> $AppID successfully uninstalled." " Green"
299+ }
300+ else {
301+ Write-ToLog " -> $AppID uninstallation failed with Exit Code: $exitCode . Retrying... (Retry $retryCount of $maxRetries )" " Red"
302+ Start-Sleep 5
303+ }
304+ }
305+
306+ if (-not $uninstallSuccess ) {
307+ Write-ToLog " -> $AppID uninstallation failed after $ ( $maxRetries + 1 ) attempts!" " Red"
308+ }
309+
310+ # Apply post-uninstallation modifications if the uninstallation was successful
311+ if ($uninstallSuccess ) {
312+ if ($ModsUninstall ) {
313+ Write-ToLog " -> Modifications for $AppID during uninstall are being applied..." " Yellow"
314+ & " $ModsUninstall "
315+ }
236316
237- # Check if uninstall is ok
238- $IsInstalled = Confirm-Installation $AppID
239- if (! ($IsInstalled )) {
240- Write-ToLog " -> $AppID successfully uninstalled." " Green"
241317 if ($ModsUninstalled ) {
242318 Write-ToLog " -> Modifications for $AppID after uninstall are being applied..." " Yellow"
243319 & " $ModsUninstalled "
244320 }
245321
246- # Remove mods if deployed from Winget-Install
247- if (Test-Path " .\mods\$AppID -*" ) {
248- # Check if WAU default install path exists
249- $Mods = " $WAUModsLocation "
250- if (Test-Path " $Mods \$AppID *" ) {
251- Write-ToLog " -> Remove $AppID modifications from WAU 'mods'"
252- # Remove mods
253- Remove-Item - Path " $Mods \$AppID -*" - Exclude " *uninstall*" - Force
254- }
322+ # Remove leftover files and folders
323+ $AppDataPath = " $env: LOCALAPPDATA \$AppID "
324+ if (Test-Path $AppDataPath ) {
325+ Write-ToLog " -> Removing leftover files for $AppID ..." " Yellow"
326+ Remove-Item - Path $AppDataPath - Recurse - Force - ErrorAction SilentlyContinue
255327 }
256-
257- # Remove from WAU White List if set
258- if ($WAUWhiteList ) {
259- Remove-WAUWhiteList $AppID
260- }
261- }
262- else {
263- Write-ToLog " -> $AppID uninstall failed!" " Red"
264328 }
265329 }
266330 else {
@@ -406,6 +470,11 @@ if ($Winget) {
406470 Start-Sleep 1
407471 }
408472}
473+ else {
474+ Write-ToLog " Winget command not found!`n " " Red"
475+ $Script :ExitCode = 1
476+ }
409477
410478Write-ToLog " ### END REQUEST ###`n " " Magenta"
411479Start-Sleep 3
480+ Exit $ExitCode
0 commit comments