#Requires -Version 5.1 <# .SYNOPSIS Baut den NexusRMM Agent für Windows (und optional Linux/amd64) .DESCRIPTION Sucht die Go-Binary, baut den Agent für Windows und optional für Linux. Erstellt eine Standard-Konfigurationsdatei, wenn noch keine vorhanden ist. .PARAMETER Linux Baut zusätzlich eine Linux-Binary (nexus-agent-linux). .EXAMPLE .\build-agent.ps1 .\build-agent.ps1 -Linux #> param( [switch]$Linux ) $ErrorActionPreference = "Stop" $AgentDir = "D:\001_Projekte\IT_Tool\Agent" $ConfigFile = "$AgentDir\nexus-agent.yaml" # --------------------------------------------------------------------------- # Helper # --------------------------------------------------------------------------- function Write-Step { param($msg) Write-Host "`n==> $msg" -ForegroundColor Cyan } function Write-OK { param($msg) Write-Host " [OK] $msg" -ForegroundColor Green } function Write-Warn { param($msg) Write-Host " [!] $msg" -ForegroundColor Yellow } function Write-Fail { param($msg) Write-Host " [FEHLER] $msg" -ForegroundColor Red } function Format-FileSize { param([long]$Bytes) if ($Bytes -ge 1MB) { return "{0:N1} MB" -f ($Bytes / 1MB) } return "{0:N0} KB" -f ($Bytes / 1KB) } # --------------------------------------------------------------------------- # 1. Go-Binary finden # --------------------------------------------------------------------------- Write-Step "Suche Go-Installation..." # Kandidaten: expliziter Standardpfad zuerst, dann PATH $goCandidates = @( "C:\Program Files\Go\bin\go.exe", "C:\Go\bin\go.exe" ) $goExe = $null foreach ($candidate in $goCandidates) { if (Test-Path $candidate) { $goExe = $candidate break } } if ($null -eq $goExe) { # Fallback: PATH $fromPath = Get-Command "go" -ErrorAction SilentlyContinue if ($null -ne $fromPath) { $goExe = $fromPath.Source } } if ($null -eq $goExe) { Write-Fail "Go wurde nicht gefunden. Bitte Go installieren (https://go.dev/dl/) oder PATH prüfen." exit 1 } $goVersion = & $goExe version Write-OK "Go gefunden: $goExe" Write-OK "Version: $goVersion" # --------------------------------------------------------------------------- # 2. Ins Agent-Verzeichnis wechseln # --------------------------------------------------------------------------- if (-not (Test-Path $AgentDir)) { Write-Fail "Agent-Verzeichnis nicht gefunden: $AgentDir" exit 1 } Push-Location $AgentDir try { # ----------------------------------------------------------------------- # 3. Windows-Build # ----------------------------------------------------------------------- Write-Step "Baue Windows-Binary (nexus-agent.exe)..." $env:GOOS = "windows" $env:GOARCH = "amd64" $env:CGO_ENABLED = "0" $winOut = "$AgentDir\nexus-agent.exe" & $goExe build -ldflags="-s -w" -o $winOut ./cmd/agent/ if ($LASTEXITCODE -ne 0) { Write-Fail "Windows-Build fehlgeschlagen." exit 1 } $winSize = (Get-Item $winOut).Length Write-OK "Windows-Binary erstellt:" Write-OK " Pfad: $winOut" Write-OK " Groesse: $(Format-FileSize $winSize)" # ----------------------------------------------------------------------- # 4. Linux-Build (optional) # ----------------------------------------------------------------------- if ($Linux) { Write-Step "Baue Linux-Binary (nexus-agent-linux)..." $env:GOOS = "linux" $env:GOARCH = "amd64" $env:CGO_ENABLED = "0" $linuxOut = "$AgentDir\nexus-agent-linux" & $goExe build -ldflags="-s -w" -o $linuxOut ./cmd/agent/ if ($LASTEXITCODE -ne 0) { Write-Fail "Linux-Build fehlgeschlagen." exit 1 } $linuxSize = (Get-Item $linuxOut).Length Write-OK "Linux-Binary erstellt:" Write-OK " Pfad: $linuxOut" Write-OK " Groesse: $(Format-FileSize $linuxSize)" } else { Write-Warn "Linux-Build übersprungen. Mit -Linux Parameter ausführen um Linux-Binary zu bauen." } } finally { # Umgebungsvariablen zurücksetzen Remove-Item Env:\GOOS -ErrorAction SilentlyContinue Remove-Item Env:\GOARCH -ErrorAction SilentlyContinue Remove-Item Env:\CGO_ENABLED -ErrorAction SilentlyContinue Pop-Location } # --------------------------------------------------------------------------- # 5. Konfigurationsdatei erstellen (nur wenn nicht vorhanden) # --------------------------------------------------------------------------- Write-Step "Prüfe Konfigurationsdatei..." if (Test-Path $ConfigFile) { Write-OK "Konfiguration bereits vorhanden: $ConfigFile (wird nicht überschrieben)" } else { Write-Warn "Keine Konfigurationsdatei gefunden. Erstelle Standard-Konfiguration..." $defaultConfig = @" # NexusRMM Agent Konfiguration server_address: localhost:5001 heartbeat_interval: 30 tls_enabled: false mesh_enabled: false mesh_central_url: "" "@ Set-Content -Path $ConfigFile -Value $defaultConfig -Encoding UTF8 Write-OK "Konfiguration erstellt: $ConfigFile" } # --------------------------------------------------------------------------- # 6. Zusammenfassung # --------------------------------------------------------------------------- Write-Host "" Write-Host "Build abgeschlossen." -ForegroundColor Green Write-Host "Agent starten: .\Agent\nexus-agent.exe" -ForegroundColor Yellow Write-Host "Konfiguration: $ConfigFile" -ForegroundColor Yellow