param( [string]$ServerId, [string]$Token, [string]$ServerUrl, [string]$InstallDir ) $ErrorActionPreference = "Stop" $logDir = Join-Path $env:ProgramData "vantage" New-Item -ItemType Directory -Force -Path $logDir | Out-Null $log = Join-Path $logDir "install.log" function Write-Log($msg) { $line = "{0} {1}" -f (Get-Date -Format "s"), $msg Add-Content -Path $log -Value $line } # Fail native-exe (nssm) calls loudly: check $LASTEXITCODE after each call function Invoke-Native { param([string]$File, [string[]]$Args) Write-Log ("RUN: {0} {1}" -f $File, ($Args -join " ")) $out = & $File @Args 2>&1 if ($out) { Write-Log ("OUT: {0}" -f ($out -join "`n")) } if ($LASTEXITCODE -ne 0) { throw ("{0} exited {1}" -f $File, $LASTEXITCODE) } } try { Write-Log "=== setup start ===" Write-Log ("ServerId={0} ServerUrl={1} InstallDir={2}" -f $ServerId, $ServerUrl, $InstallDir) $cfgDir = Join-Path $env:ProgramData "vantage" New-Item -ItemType Directory -Force -Path $cfgDir | Out-Null $cfg = @" server_url: "$ServerUrl" server_id: "$ServerId" pre_reg_token: "$Token" agent_token: "" poll_interval: 30s tls: true "@ $cfgPath = Join-Path $cfgDir "config.yaml" Set-Content -Path $cfgPath -Value $cfg -Encoding utf8 Write-Log "wrote $cfgPath" # Lock down ACL: SYSTEM + Administrators only Invoke-Native -File "icacls" -Args @($cfgPath, "/inheritance:r", "/grant:r", "SYSTEM:F", "Administrators:F") if (-not $InstallDir) { $InstallDir = $PSScriptRoot } $nssm = Join-Path $InstallDir "nssm.exe" $exe = Join-Path $InstallDir "vantage-agent.exe" if (-not (Test-Path $nssm)) { throw "nssm.exe not found at $nssm" } if (-not (Test-Path $exe)) { throw "vantage-agent.exe not found at $exe" } Invoke-Native -File $nssm -Args @("install", "VantageAgent", $exe) Invoke-Native -File $nssm -Args @("set", "VantageAgent", "Start", "SERVICE_AUTO_START") Invoke-Native -File $nssm -Args @("start", "VantageAgent") Write-Log "=== setup ok ===" exit 0 } catch { Write-Log ("ERROR: {0}" -f $_.Exception.Message) Write-Log ($_.ScriptStackTrace) exit 1 }