$out = "G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\_deployment\phase6_start_results.txt" $log = "G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\publish\logs\attendancehub_sync.log" $r = New-Object System.Collections.Generic.List[string] $r.Add("elevatedAt=$(Get-Date -Format o)") $r.Add("identity=$([Security.Principal.WindowsIdentity]::GetCurrent().Name)") $isAdmin = (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) $r.Add("is_admin=$isAdmin") # capture pre-start log offset passed via marker file (written by non-elevated shell before launch) $offFile = "G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\_deployment\log_offset.txt" $off = 0 if (Test-Path $offFile) { $off = [int64](Get-Content $offFile -Raw).Trim() } $r.Add("log_offset_before=$off") $r.Add("---- START") try { Start-Service ZKTecoSyncService -ErrorAction Stop; $r.Add("start_cmd=ISSUED") } catch { $r.Add("start_cmd=FAILED error=$($_.Exception.Message)") } $sw = [System.Diagnostics.Stopwatch]::StartNew() while ((Get-Service ZKTecoSyncService).Status -ne 'Running' -and $sw.Elapsed.TotalSeconds -lt 45) { Start-Sleep -Seconds 1 } $svc = Get-CimInstance Win32_Service -Filter "Name='ZKTecoSyncService'" $r.Add("state_final=" + (Get-Service ZKTecoSyncService).Status) $r.Add("pid_final=$($svc.ProcessId)") # wait for startup log lines (host start -> LIVE/READ ready -> upload activation) $r.Add("---- WAIT for health tokens (max 90s)") $deadline = (Get-Date).AddSeconds(90) $gotLive = $false; $gotRead = $false; $gotUpload = $false; $gotLoop = $false while ((Get-Date) -lt $deadline -and -not ($gotLive -and $gotRead -and $gotUpload)) { Start-Sleep -Seconds 2 if (-not (Test-Path $log)) { continue } $fs = [System.IO.File]::Open($log, 'Open', 'Read', 'ReadWrite') try { $fs.Seek($off, 'Begin') | Out-Null $sr = New-Object System.IO.StreamReader($fs) $chunk = $sr.ReadToEnd() $sr.Close() } finally { $fs.Close() } if (-not $gotLive) { $gotLive = $chunk -match 'LIVE_READY ' } if (-not $gotRead) { $gotRead = $chunk -match 'READ_READY ' } if (-not $gotUpload) { $gotUpload = $chunk -match 'UPLOAD_ACTIVATED ' } if (-not $gotLoop) { $gotLoop = $chunk -match 'UPLOAD_LOOP_START ' } } $r.Add("LIVE_READY_seen=$gotLive") $r.Add("READ_READY_seen=$gotRead") $r.Add("UPLOAD_ACTIVATED_seen=$gotUpload") $r.Add("UPLOAD_LOOP_START_seen=$gotLoop") # extract key lines from new log content $fs2 = [System.IO.File]::Open($log, 'Open', 'Read', 'ReadWrite') try { $fs2.Seek($off, 'Begin') | Out-Null $sr2 = New-Object System.IO.StreamReader($fs2) $newContent = $sr2.ReadToEnd() $sr2.Close() } finally { $fs2.Close() } $lines = $newContent -split "`r?`n" foreach ($pat in @('PHASE1_HOST_START','UPLOAD_ACTIVATED','LIVE_READY','READ_READY','WORKER_START','UPLOAD_LOOP_START','UPLOAD_BATCH','UPLOAD_CYCLE','SESSION_LIVE_RECREATE','SESSION_READ_RECREATE','LIVE_SESSION_RECREATED','READ_SESSION_RECREATED','CROSS_THREAD_COM_ACCESS','UPLOAD_CONFIG_PARSE_FAILED','UPLOAD_ACTIVATION_FAILED','HOST_STOP','FATAL','Unhandled')) { $hits = @($lines | Where-Object { $_ -match $pat }) $r.Add("pat=$pat count=$($hits.Count)") foreach ($h in $hits | Select-Object -First 3) { $r.Add(" | $h") } } Set-Content -Path $out -Value ($r -join "`r`n") -Encoding UTF8 $r.Add("results_written=$out")