[CmdletBinding()] param([int]$Port = 0) $ErrorActionPreference = 'Stop' [Console]::OutputEncoding = [Text.UTF8Encoding]::new($false) if ($Port -eq 0) { $Port = if ($env:AGENT_WORKBENCH_PORT) { [int]$env:AGENT_WORKBENCH_PORT } else { 4318 } } if ($Port -lt 1 -or $Port -gt 65535) { throw 'Please use a port between 1 and 65535.' } $workbenchUrl = "http://127.0.0.1:$Port" try { $health = Invoke-RestMethod -Uri "$workbenchUrl/api/health" -TimeoutSec 2 } catch { Write-Output "Agent Workbench is not running on port $Port."; exit 0 } if ($health.app -ne 'agent-workbench' -or -not $health.ready) { throw "This port is occupied by another service: $Port" } $bootstrap = Invoke-RestMethod -Uri "$workbenchUrl/api/bootstrap" -TimeoutSec 3 Invoke-RestMethod -Uri "$workbenchUrl/api/shutdown" -Method Post -ContentType 'application/json' -Body '{}' -Headers @{ 'X-Workbench-Token' = $bootstrap.token } -TimeoutSec 15 | Out-Null Write-Output "Agent Workbench is stopping: $workbenchUrl"