Enhance sync_data.ps1: add shutdown option for failed jobs and improve error handling

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-04-26 14:06:45 -04:00
parent 814ea84098
commit f637c53930
+27 -3
View File
@@ -1,5 +1,11 @@
#Requires -Version 5.1
# Use -ShutdownRegardless to force shutdown even if one or more sync jobs fail.
param(
[switch]$ShutdownRegardless
)
$FfsExe = 'C:\Program Files\FreeFileSync\FreeFileSync.exe'
$ScriptDir = $PSScriptRoot
$LogDir = Join-Path $ScriptDir 'logs'
@@ -30,6 +36,9 @@ Write-Log 'Sync run started'
Write-Host "Log file: $LogFile"
# ── Run sync jobs ──────────────────────────────────────────────────────────────
$HadFatalError = $false
$FatalExitCodes = @()
foreach ($job in $SyncJobs) {
$jobName = Split-Path $job -Leaf
Write-Host ''
@@ -48,7 +57,9 @@ foreach ($job in $SyncJobs) {
1 { Write-Log 'Synchronization completed with warnings.' }
default {
Write-Log "Synchronization failed or was aborted. Exit code: $rc"
exit $rc
$HadFatalError = $true
$FatalExitCodes += $rc
continue
}
}
}
@@ -64,8 +75,8 @@ $logPaths = Select-String -Path $LogFile -Pattern '"logFile"\s*:\s*"([^"]+)"' |
$copied = 0
foreach ($path in $logPaths) {
if ([IO.Path]::GetExtension($path) -ieq '.html') {
if (Test-Path $path) {
Copy-Item -Path $path -Destination $ReportDir -Force
if (Test-Path -LiteralPath $path) {
Copy-Item -LiteralPath $path -Destination $ReportDir -Force
Write-Log "Copied report: $(Split-Path $path -Leaf)"
$copied++
} else {
@@ -77,5 +88,18 @@ foreach ($path in $logPaths) {
Write-Log "Total reports copied: $copied"
# ── Shutdown ───────────────────────────────────────────────────────────────────
if ($HadFatalError) {
$codes = ($FatalExitCodes | Sort-Object -Unique) -join ', '
Write-Log "One or more synchronization jobs failed. Fatal exit code(s): $codes"
if (-not $ShutdownRegardless) {
Write-Log 'Skipping shutdown because not all synchronization jobs completed successfully.'
Write-Log 'Use -ShutdownRegardless to force shutdown even when jobs fail.'
exit ($FatalExitCodes | Select-Object -Last 1)
}
Write-Log 'Forced shutdown enabled. Continuing to shutdown despite synchronization failures.'
}
Write-Log 'Shutting down server now.'
Stop-Computer -Force