param( [Parameter(Mandatory = $true)] [string]$Server, [Parameter(Mandatory = $false)] [string]$Token = "none", [Parameter(Mandatory = $false)] [int]$Interval = 10 ) $ErrorActionPreference = "Stop" function Assert-Admin { $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = New-Object Security.Principal.WindowsPrincipal($identity) if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { throw "请使用管理员 PowerShell 执行安装脚本" } } function Normalize-Server([string]$Value) { return $Value.TrimEnd("/") } Assert-Admin $Server = Normalize-Server $Server $InstallDir = "C:\Program Files\AIOps" $LogDir = "C:\ProgramData\AIOps\logs" $AgentPath = Join-Path $InstallDir "aiops-agent.exe" $ServiceName = "AIOpsAgent" $BinaryUrl = "$Server/agent/aiops-agent-windows-amd64.exe" Write-Host "" Write-Host "AIOps Agent Windows 安装" Write-Host "Server: $Server" Write-Host "Binary: $BinaryUrl" Write-Host "" New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null New-Item -ItemType Directory -Force -Path $LogDir | Out-Null $Tmp = Join-Path $env:TEMP "aiops-agent-windows-amd64.exe" Invoke-WebRequest -Uri $BinaryUrl -OutFile $Tmp -UseBasicParsing Copy-Item -Force $Tmp $AgentPath $Args = "-server `"$Server`" -interval $Interval -token `"$Token`"" $Existing = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue if ($Existing) { Stop-Service -Name $ServiceName -Force -ErrorAction SilentlyContinue sc.exe delete $ServiceName | Out-Null Start-Sleep -Seconds 1 } New-Service -Name $ServiceName ` -DisplayName "AIOps Agent" ` -Description "AIOps Windows monitoring and task agent" ` -BinaryPathName "`"$AgentPath`" $Args" ` -StartupType Automatic | Out-Null Start-Service -Name $ServiceName Start-Sleep -Seconds 2 $Status = (Get-Service -Name $ServiceName).Status if ($Status -ne "Running") { throw "服务启动失败,当前状态: $Status" } Write-Host "" Write-Host "安装成功,AIOps Agent 已作为 Windows 服务启动" Write-Host "查看状态: Get-Service AIOpsAgent" Write-Host "停止服务: Stop-Service AIOpsAgent" Write-Host "卸载服务: Stop-Service AIOpsAgent; sc.exe delete AIOpsAgent"