$ErrorActionPreference = "Stop"
$TaskName = "NT Workbench Local Runner"
$WorkBenchUrl = "https://zhuanzhuan-nt.cn/"

if (!(Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue)) {
  Write-Host "还没有安装本机执行器。请先双击“第一步-双击安装并启动.bat”。"
  exit 1
}

Start-ScheduledTask -TaskName $TaskName
Start-Process $WorkBenchUrl
for ($i = 0; $i -lt 10; $i++) {
  try {
    Invoke-WebRequest -UseBasicParsing "http://127.0.0.1:4318/health" -TimeoutSec 2 | Out-Null
    Write-Host "本机执行器已启动，工作台已打开。"
    exit 0
  } catch {
    Start-Sleep -Seconds 1
  }
}

Write-Host "本机执行器暂时没有启动成功。请重新运行“第一步-双击安装并启动.bat”，或把窗口截图发给负责人。"
exit 1
