$ErrorActionPreference = 'Stop' $root = [System.IO.Path]::GetFullPath($PSScriptRoot) $config = Get-Content -LiteralPath (Join-Path $root 'agent.json') -Raw | ConvertFrom-Json $tokenFile = Join-Path $root 'agent.env' foreach ($line in Get-Content -LiteralPath $tokenFile) { if ($line.StartsWith('ADS_AGENT_TOKEN=')) { $env:ADS_AGENT_TOKEN = $line.Substring(16) } } if (-not $env:ADS_AGENT_TOKEN -or $env:ADS_AGENT_TOKEN.Length -lt 32) { throw 'Missing agent token' } if (-not $config.server.StartsWith('wss://')) { throw 'Public relay requires WSS' } $env:RUST_LOG = 'info' $logDirectory = Join-Path $root 'logs' New-Item -ItemType Directory -Path $logDirectory -Force | Out-Null $logFile = Join-Path $logDirectory 'agent.log' if ((Test-Path -LiteralPath $logFile) -and (Get-Item -LiteralPath $logFile).Length -gt 10MB) { Move-Item -LiteralPath $logFile -Destination ($logFile + '.1') -Force } $arguments = if ($config.id -and $config.devices -ne $null) { @('--quiet', '--supervise', '--config', (Join-Path $root 'agent.json')) } else { @('--server', [string]$config.server, '--device', [string]$config.device, '--target', [string]$config.target) } if (-not $config.id -and $config.connect_address) { $arguments += @('--connect-address', [string]$config.connect_address) } # Keep the child in this task's process tree. Task Scheduler owns its lifecycle. # Tracing writes to stdout; stderr is also retained for startup diagnostics. $ErrorActionPreference = 'Continue' & (Join-Path $root 'ads-agent.exe') @arguments >> $logFile 2>&1 exit $LASTEXITCODE