# Reinstall Task Scheduler voi cac setting nang cao: # - StartWhenAvailable: tu chay bu neu may lo gio scheduled # - RestartOnFailure: tu thu lai neu loi (3 lan, moi lan cach 10 phut) # - ExecutionTimeLimit: toi da 1 gio $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path $batFile = Join-Path $scriptDir 'auto_daily_sync.bat' if (-not (Test-Path $batFile)) { Write-Host "[!] Khong tim thay: $batFile" -ForegroundColor Red exit 1 } Write-Host "Script dir: $scriptDir" -ForegroundColor Cyan Write-Host "Bat file : $batFile" -ForegroundColor Cyan Write-Host "" # Xoa task cu Write-Host "Xoa task cu..." -ForegroundColor Yellow schtasks /delete /tn "AttendanceAutoSync_Morning" /f 2>$null schtasks /delete /tn "AttendanceAutoSync_Evening" /f 2>$null # Tao action $action = New-ScheduledTaskAction -Execute $batFile -WorkingDirectory $scriptDir # Tao settings voi StartWhenAvailable + RestartOnFailure $settings = New-ScheduledTaskSettingsSet ` -StartWhenAvailable ` -ExecutionTimeLimit (New-TimeSpan -Hours 1) ` -RestartCount 3 ` -RestartInterval (New-TimeSpan -Minutes 10) ` -MultipleInstances IgnoreNew # Tao principal SYSTEM $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest -LogonType ServiceAccount # === MORNING 09:30 === Write-Host "[1/2] Tao Morning task (09:30)..." -ForegroundColor Green $triggerM = New-ScheduledTaskTrigger -Daily -At "09:30" Register-ScheduledTask ` -TaskName "AttendanceAutoSync_Morning" ` -Action $action ` -Trigger $triggerM ` -Settings $settings ` -Principal $principal ` -Description "Sync cham cong sang - hom qua + hom nay (catch-up + Vao)" ` -Force | Out-Null if ($?) { Write-Host " -> Da tao Morning task OK" -ForegroundColor Green } else { Write-Host " -> LOI tao Morning task!" -ForegroundColor Red } # === EVENING 19:45 === Write-Host "[2/2] Tao Evening task (19:45)..." -ForegroundColor Green $triggerE = New-ScheduledTaskTrigger -Daily -At "19:45" Register-ScheduledTask ` -TaskName "AttendanceAutoSync_Evening" ` -Action $action ` -Trigger $triggerE ` -Settings $settings ` -Principal $principal ` -Description "Sync cham cong toi - Ra sau tan ca (retry tu dong neu device offline)" ` -Force | Out-Null if ($?) { Write-Host " -> Da tao Evening task OK" -ForegroundColor Green } else { Write-Host " -> LOI tao Evening task!" -ForegroundColor Red } # Verify Write-Host "" Write-Host "=== Ket qua ===" -ForegroundColor Cyan Get-ScheduledTask -TaskName "AttendanceAutoSync_Morning" -ErrorAction SilentlyContinue | Select-Object TaskName, State, @{N='NextRun';E={(Get-ScheduledTaskInfo $_.TaskName).NextRunTime}} | Format-Table -AutoSize Get-ScheduledTask -TaskName "AttendanceAutoSync_Evening" -ErrorAction SilentlyContinue | Select-Object TaskName, State, @{N='NextRun';E={(Get-ScheduledTaskInfo $_.TaskName).NextRunTime}} | Format-Table -AutoSize Write-Host "" Write-Host "Hoan tat! Task Scheduler da duoc sua lai." -ForegroundColor Green Write-Host "Tinh nang moi:" -ForegroundColor White Write-Host " + StartWhenAvailable: tu chay bu neu may lo gio (reboot, ngu)" -ForegroundColor White Write-Host " + RestartOnFailure : tu thu lai 3 lan, moi lan cach 10 phut" -ForegroundColor White Write-Host " + Sync hom qua + hom nay de catch-up neu toi truoc bi loi" -ForegroundColor White Write-Host " + Retry 20 phut neu device Hikvision dang offline" -ForegroundColor White