Files
wehub-resource-sync 3e779be6f3
CI / test (3.14, ubuntu-24.04-arm) (push) Waiting to run
CI / lint (push) Failing after 13m4s
CI / test (3.11, ubuntu-latest) (push) Failing after 2m4s
CI / test (3.13, ubuntu-latest) (push) Successful in 13m30s
CI / test (3.14, ubuntu-latest) (push) Successful in 17m21s
CI / test (3.12, ubuntu-latest) (push) Successful in 17m55s
CI / discover-apps-ps (push) Successful in 1m56s
CI / test (3.9, ubuntu-latest) (push) Successful in 13m17s
CI / test (3.10, ubuntu-latest) (push) Successful in 26m21s
CI / audit (push) Successful in 13m38s
Deploy site / deploy (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:32:37 +08:00

44 lines
1.5 KiB
PowerShell

# SPDX-License-Identifier: MIT
# Synchronize Windows time with host
# Run on Windows startup to prevent clock drift after sleep/wake
$ntpServers = @("time.windows.com", "pool.ntp.org")
$maxAttempts = 3
$retryDelaySeconds = 5
$synced = $false
foreach ($server in $ntpServers) {
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
try {
# w32tm returns a non-zero exit code on failure without throwing, so success must be checked via $LASTEXITCODE.
w32tm /config /manualpeerlist:$server /syncfromflags:manual /reliable:YES /update | Out-Null
$configExit = $LASTEXITCODE
w32tm /resync /force | Out-Null
$resyncExit = $LASTEXITCODE
if ($configExit -eq 0 -and $resyncExit -eq 0) {
Write-Host "Time synced with $server"
$synced = $true
break
}
Write-Host ("Sync with {0} failed (config={1}, resync={2}), attempt {3}/{4}" -f `
$server, $configExit, $resyncExit, $attempt, $maxAttempts)
} catch {
Write-Host "Sync with $server threw: $($_.Exception.Message) (attempt $attempt/$maxAttempts)"
}
if ($attempt -lt $maxAttempts) {
Start-Sleep -Seconds $retryDelaySeconds
}
}
if ($synced) { break }
Write-Host "Giving up on $server, trying next server..."
}
if (-not $synced) {
Write-Host "Time sync failed for all configured NTP servers."
exit 1
}