Skip to content

Commit f8bb371

Browse files
committed
Error handling
1 parent b1131ef commit f8bb371

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

Sources/Winget-AutoUpdate/Winget-Upgrade.ps1

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -345,45 +345,76 @@ if (Test-Network) {
345345
}
346346
"Postpone" {
347347
Write-ToLog "Mods requested a postpone of WAU"
348-
$postponeDuration = if ($ModsResult.PostponeDuration) {
349-
$ModsResult.PostponeDuration
350-
} else {
351-
1
348+
# Check if a postponed task already exists
349+
$existingTask = Get-ScheduledTask -TaskPath "\WAU\" -ErrorAction SilentlyContinue | Where-Object { $_.TaskName -like "Postponed-$($Script:GitHub_Repo)*" }
350+
if ($existingTask) {
351+
Write-ToLog "A postponed task for $($Script:GitHub_Repo) already exists, not creating another." "Yellow"
352+
}
353+
else {
354+
# Get configurable duration, default to 1 hour
355+
$postponeDuration = if ($ModsResult.PostponeDuration) {
356+
try {
357+
[double]$parsedDuration = [double]$ModsResult.PostponeDuration
358+
# Ensure minimum duration of 0.1 hours (6 minutes)
359+
if ($parsedDuration -lt 0.1) {
360+
Write-ToLog "PostponeDuration adjusted to minimum 0.1 hours (6 minutes)" "Yellow"
361+
0.1
362+
} else {
363+
$parsedDuration
364+
}
365+
}
366+
catch {
367+
Write-ToLog "Invalid PostponeDuration value '$($ModsResult.PostponeDuration)', using default 1 hour" "Yellow"
368+
1
369+
}
370+
} else {
371+
1
372+
}
373+
374+
# Create a postponed temporary scheduled task to try again later
375+
$uniqueTaskName = "Postponed-$($Script:GitHub_Repo)_$(Get-Random)"
376+
$taskPath = "\WAU\"
377+
$copyAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$($WAUConfig.InstallLocation)Winget-Upgrade.ps1`""
378+
$copyTrigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddHours($postponeDuration)
379+
# Set EndBoundary to make DeleteExpiredTaskAfter work
380+
$copyTrigger.EndBoundary = (Get-Date).AddHours($postponeDuration).AddMinutes(1).ToString("yyyy-MM-ddTHH:mm:ss")
381+
$copySettings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -ExecutionTimeLimit (New-TimeSpan -Minutes 60) -DeleteExpiredTaskAfter (New-TimeSpan -Seconds 0)
382+
$copyPrincipal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
383+
Register-ScheduledTask -TaskName $uniqueTaskName -TaskPath $taskPath -Action $copyAction -Trigger $copyTrigger -Settings $copySettings -Principal $copyPrincipal -Description "Postponed copy of $Script:GitHub_Repo" | Out-Null
384+
Write-ToLog "WAU will try again in $postponeDuration hours" "Yellow"
352385
}
353-
# Create a postponed temporary scheduled task to try again later
354-
$uniqueTaskName = "Postponed-$($Script:GitHub_Repo)_$(Get-Random)"
355-
$taskPath = "\WAU\"
356-
$copyAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$($WAUConfig.InstallLocation)winget-upgrade.ps1`""
357-
$copyTrigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddHours($postponeDuration)
358-
# Set EndBoundary to make DeleteExpiredTaskAfter work
359-
$copyTrigger.EndBoundary = (Get-Date).AddHours($postponeDuration).AddMinutes(1).ToString("yyyy-MM-ddTHH:mm:ss")
360-
$copySettings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -ExecutionTimeLimit (New-TimeSpan -Minutes 60) -DeleteExpiredTaskAfter (New-TimeSpan -Seconds 0)
361-
$copyPrincipal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
362-
Register-ScheduledTask -TaskName $uniqueTaskName -TaskPath $taskPath -Action $copyAction -Trigger $copyTrigger -Settings $copySettings -Principal $copyPrincipal -Description "Postponed copy of $Script:GitHub_Repo" | Out-Null
363-
364-
Write-ToLog "WAU will try again in $postponeDuration hours" "Yellow"
365386
$exitCode = if ($ModsResult.ExitCode) { $ModsResult.ExitCode } else { 1602 } # Default to "User cancelled"
366387
Exit $exitCode
367388
}
368389
"Reboot" {
369390
Write-ToLog "Mods requested a system reboot"
370-
371391
# Get configurable delay, default to 5 minutes
372-
$rebootDelay = if ($ModsResult.RebootDelay) {
373-
$ModsResult.RebootDelay
392+
$rebootDelay = if ($ModsResult.RebootDelay) {
393+
try {
394+
[double]$parsedDelay = [double]$ModsResult.RebootDelay
395+
# Ensure minimum delay of 1 minute for safety
396+
if ($parsedDelay -lt 1) {
397+
Write-ToLog "RebootDelay adjusted to minimum 1 minute" "Yellow"
398+
1
399+
} else {
400+
$parsedDelay
401+
}
402+
}
403+
catch {
404+
Write-ToLog "Invalid RebootDelay value '$($ModsResult.RebootDelay)', using default 5 minutes" "Yellow"
405+
5
406+
}
374407
} else {
375408
5
376409
}
377410

378-
# Ensure minimum delay of 1 minute for safety
379-
if ($rebootDelay -lt 1) {
380-
$rebootDelay = 1
381-
Write-ToLog "Reboot delay adjusted to minimum 1 minute" "Yellow"
382-
}
383-
384411
$shutdownMessage = if ($ModsResult.Message) { $ModsResult.Message } else { "WAU Mods requested a system reboot in $rebootDelay minutes" }
385-
& shutdown /r /t ([int]($rebootDelay * 60)) /c $shutdownMessage
386-
Write-ToLog "System restart scheduled in $rebootDelay minutes" "Yellow"
412+
$result = & shutdown /r /t ([int]($rebootDelay * 60)) /c $shutdownMessage 2>&1
413+
if ([string]::IsNullOrEmpty($result)) {
414+
Write-ToLog "System restart scheduled in $rebootDelay minutes" "Yellow"
415+
} else {
416+
Write-ToLog "A system shutdown has already been scheduled" "Yellow"
417+
}
387418
$exitCode = if ($ModsResult.ExitCode) { $ModsResult.ExitCode } else { 3010 } # Default to "Restart required"
388419
Exit $exitCode
389420
}

Sources/Winget-AutoUpdate/mods/_WAU-mods-template.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
"Message": "string", // Optional: Message to write to WAU log
2121
"LogLevel": "string", // Optional: Log level for the message
2222
"ExitCode": number, // Optional: Windows installer exit code for reference
23-
"PostponeDuration": number, // Optional: Postpone in hours before running WAU again (default 1 hour)
23+
"PostponeDuration": number, // Optional: Postpone duration in hours before running WAU again (default 1 hour)
2424
"RebootDelay": number // Optional: Delay in minutes before rebooting (default 5 minutes)
2525
}
2626
2727
Available Actions:
2828
- "Continue" : Continue with normal WAU execution (default behavior)
2929
- "Abort" : Abort WAU execution completely
30-
- "Postpone" : Delay WAU execution temporarily with 'PostponeDuration' hours
30+
- "Postpone" : Postpone WAU execution temporarily with 'PostponeDuration' hours
3131
- "Rerun" : Re-run WAU (equivalent to legacy exit code 1)
3232
- "Reboot" : Restart the system with delay and notification to end user
3333

0 commit comments

Comments
 (0)