diff --git a/install-update-zabbix-agent.ps1 b/install-update-zabbix-agent.ps1 index 248c08c..83c8c56 100644 --- a/install-update-zabbix-agent.ps1 +++ b/install-update-zabbix-agent.ps1 @@ -1,6 +1,6 @@ #Requires -RunAsAdministrator # ============================================================================= -# Zabbix Agent 2 – Windows Installer / Updater (Version 7.4) +# Zabbix Agent 2 - Windows Installer / Updater # # Aufruf: # PowerShell -ExecutionPolicy Bypass -File install-update-zabbix-agent.ps1 @@ -8,40 +8,52 @@ # Interaktiv: Proxy-Auswahl, Hostname-Eingabe, automatische Install/Update-Erkennung # ============================================================================= -# ── Konfiguration (manuell anpassen) ───────────────────────────────────────── +# -------------------------------------------------------------------------- +# Konfiguration (manuell anpassen) +# -------------------------------------------------------------------------- +<<<<<<< Updated upstream $ZabbixServer = "zabbix.server-nb.de" # Zabbix-Server-Adresse $ZabbixVersion = "7.4.7" # Zabbix-Version (bei neuen Releases anpassen) +======= +$ScriptVersion = "1.2" # Skript-Version +>>>>>>> Stashed changes -# Proxy-Liste – hier manuell pflegen (Name und URL) +$ZabbixServer = "zabbix.server-nb.de" # Zabbix-Server-Adresse +$ZabbixVersion = "7.4.7" # Zabbix Agent-Version (bei neuen Releases anpassen) + +# Proxy-Liste - hier manuell pflegen (Name und URL) $ProxyList = @( - @{ Name = "Standort Büro A"; Url = "http://proxy01.example.com:3128" } - @{ Name = "Standort Büro B"; Url = "http://proxy02.example.com:3128" } + @{ Name = "Standort Buero A"; Url = "http://proxy01.example.com:3128" } + @{ Name = "Standort Buero B"; Url = "http://proxy02.example.com:3128" } # weitere Proxys einfach hier eintragen: # @{ Name = "Rechenzentrum"; Url = "http://proxy03.example.com:3128" } ) -# Installationspfade (normalerweise nicht ändern nötig) -$InstallDir = "C:\Program Files\Zabbix Agent 2" -$ConfigFile = "$InstallDir\zabbix_agent2.conf" +# Installationspfade (normalerweise nicht aendern noetig) $ServiceName = "Zabbix Agent 2" $TempDir = $env:TEMP -# ── Hilfsfunktionen ─────────────────────────────────────────────────────────── +# -------------------------------------------------------------------------- +# Hilfsfunktionen +# -------------------------------------------------------------------------- -function Write-Info { param($msg) Write-Host "[INFO] $msg" -ForegroundColor Green } -function Write-Warn { param($msg) Write-Host "[WARN] $msg" -ForegroundColor Yellow } -function Write-Err { param($msg) Write-Host "[ERROR] $msg" -ForegroundColor Red } +function Write-Info { param($msg) Write-Host "[INFO] $msg" -ForegroundColor Green } +function Write-Warn { param($msg) Write-Host "[WARN] $msg" -ForegroundColor Yellow } +function Write-Err { param($msg) Write-Host "[ERROR] $msg" -ForegroundColor Red } function Show-Header { Write-Host "" Write-Host "=============================================" -ForegroundColor Cyan - Write-Host " Zabbix Agent 2 $ZabbixVersion – Windows Setup" -ForegroundColor Cyan + Write-Host " Zabbix Agent 2 $ZabbixVersion - Windows Setup" -ForegroundColor Cyan + Write-Host " Skript-Version: $ScriptVersion" -ForegroundColor Cyan Write-Host "=============================================" -ForegroundColor Cyan Write-Host "" } -# ── Installierte Version prüfen ─────────────────────────────────────────────── +# -------------------------------------------------------------------------- +# Installierte Version pruefen +# -------------------------------------------------------------------------- function Get-InstalledVersion { $reg = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" ` @@ -52,13 +64,15 @@ function Get-InstalledVersion { return $null } -# ── Proxy-Auswahl ───────────────────────────────────────────────────────────── +# -------------------------------------------------------------------------- +# Proxy-Auswahl +# -------------------------------------------------------------------------- function Select-Proxy { Write-Host "Proxy-Einstellungen:" -ForegroundColor Cyan Write-Host " [0] Kein Proxy (direkte Verbindung)" for ($i = 0; $i -lt $ProxyList.Count; $i++) { - Write-Host " [$($i+1)] $($ProxyList[$i].Name) – $($ProxyList[$i].Url)" + Write-Host " [$($i+1)] $($ProxyList[$i].Name) - $($ProxyList[$i].Url)" } Write-Host "" @@ -68,12 +82,15 @@ function Select-Proxy { if ($choice -eq "0") { return $null - } else { + } + else { return $ProxyList[[int]$choice - 1].Url } } -# ── Download ────────────────────────────────────────────────────────────────── +# -------------------------------------------------------------------------- +# Download +# -------------------------------------------------------------------------- function Get-ZabbixMsi { param( @@ -93,22 +110,29 @@ function Get-ZabbixMsi { $proxy = New-Object System.Net.WebProxy($ProxyUrl, $true) $proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials $webClient.Proxy = $proxy - } else { + } + else { $webClient.Proxy = $null Write-Info "Direktverbindung (kein Proxy)" } $webClient.DownloadFile($Url, $Destination) Write-Info "Download abgeschlossen: $Destination" - } catch { + } + catch { Write-Err "Download fehlgeschlagen: $_" exit 1 - } finally { + } + finally { $webClient.Dispose() } } -# ── MSI Install / Update ────────────────────────────────────────────────────── +# -------------------------------------------------------------------------- +# MSI Install / Update +# Verwendet ProcessStartInfo direkt - umgeht PS5.1 Quoting-Probleme +# bei Leerzeichen in Werten (z.B. Hostname "Tischlerei Terminal") +# -------------------------------------------------------------------------- function Install-ZabbixMsi { param( @@ -120,7 +144,6 @@ function Install-ZabbixMsi { $action = if ($IsUpdate) { "Update" } else { "Installation" } Write-Info "Starte $action ..." - # Bei Update: Dienst stoppen if ($IsUpdate) { $svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue if ($svc -and $svc.Status -eq "Running") { @@ -130,28 +153,35 @@ function Install-ZabbixMsi { } } - $msiArgs = @( - "/i", $MsiPath, - "/qn", # Silent - "/l*v", "$TempDir\zabbix-install.log", - "SERVER=$ZabbixServer", - "SERVERACTIVE=$ZabbixServer", - "HOSTNAME=$Hostname", - "LISTENPORT=10050" - ) + $logFile = "$TempDir\zabbix-install.log" + $argString = "/i `"$MsiPath`" /qn /l*v `"$logFile`" " + + "SERVER=`"$ZabbixServer`" " + + "SERVERACTIVE=`"$ZabbixServer`" " + + "HOSTNAME=`"$Hostname`" " + + "LISTENPORT=10050" - $proc = Start-Process -FilePath "msiexec.exe" -ArgumentList $msiArgs -Wait -PassThru + $pinfo = New-Object System.Diagnostics.ProcessStartInfo + $pinfo.FileName = "msiexec.exe" + $pinfo.Arguments = $argString + $pinfo.UseShellExecute = $false + + $proc = New-Object System.Diagnostics.Process + $proc.StartInfo = $pinfo + $proc.Start() | Out-Null + $proc.WaitForExit() if ($proc.ExitCode -ne 0) { Write-Err "MSI $action fehlgeschlagen (ExitCode: $($proc.ExitCode))" - Write-Err "Details: $TempDir\zabbix-install.log" + Write-Err "Details: $logFile" exit 1 } Write-Info "MSI $action erfolgreich." } -# ── Dienst starten ──────────────────────────────────────────────────────────── +# -------------------------------------------------------------------------- +# Dienst starten +# -------------------------------------------------------------------------- function Start-ZabbixService { $svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue @@ -168,63 +198,57 @@ function Start-ZabbixService { return $svc.Status } -# ── Hauptprogramm ───────────────────────────────────────────────────────────── +# -------------------------------------------------------------------------- +# Hauptprogramm +# -------------------------------------------------------------------------- Show-Header -# Installierte Version ermitteln $installedVersion = Get-InstalledVersion $isUpdate = $null -ne $installedVersion if ($isUpdate) { Write-Info "Installierte Version gefunden: $installedVersion" Write-Info "Modus: UPDATE auf $ZabbixVersion" -} else { +} +else { Write-Info "Kein Zabbix Agent gefunden." Write-Info "Modus: NEUINSTALLATION von $ZabbixVersion" } Write-Host "" -# Hostname abfragen $defaultHostname = $env:COMPUTERNAME -$inputHostname = Read-Host "Zabbix-Hostname eingeben [Standard: $defaultHostname]" -$agentHostname = if ($inputHostname.Trim() -eq "") { $defaultHostname } else { $inputHostname.Trim() } +$inputHostname = Read-Host "Zabbix-Hostname eingeben [Standard: $defaultHostname]" +$agentHostname = if ($inputHostname.Trim() -eq "") { $defaultHostname } else { $inputHostname.Trim() } Write-Info "Verwende Hostname: $agentHostname" Write-Host "" -# Proxy-Auswahl $selectedProxy = Select-Proxy Write-Host "" -# MSI-Download-URL zusammenbauen $arch = "amd64" $msiFile = "zabbix_agent2-$ZabbixVersion-windows-$arch-openssl.msi" $msiUrl = "https://cdn.zabbix.com/zabbix/binaries/stable/" + "$($ZabbixVersion.Split('.')[0..1] -join '.')/$ZabbixVersion/$msiFile" $msiDest = "$TempDir\$msiFile" -# Download Get-ZabbixMsi -Url $msiUrl -Destination $msiDest -ProxyUrl $selectedProxy -# Installation / Update Install-ZabbixMsi -MsiPath $msiDest -Hostname $agentHostname -IsUpdate $isUpdate -# Temporäre Datei aufräumen Remove-Item -Path $msiDest -Force -ErrorAction SilentlyContinue -# Dienst starten Write-Info "Starte Zabbix-Dienst ..." $status = Start-ZabbixService -# Ergebnis Write-Host "" Write-Host "=============================================" -ForegroundColor Cyan Write-Info " Zabbix Agent 2 erfolgreich eingerichtet!" -Write-Info " Hostname : $agentHostname" -Write-Info " Server : $ZabbixServer" -Write-Info " Version : $ZabbixVersion" -Write-Info " Modus : Aktiv" -Write-Info " Dienst : $status" +Write-Info " Hostname : $agentHostname" +Write-Info " Server : $ZabbixServer" +Write-Info " Version : $ZabbixVersion" +Write-Info " Modus : Aktiv" +Write-Info " Dienst : $status" Write-Host "=============================================" -ForegroundColor Cyan Write-Host "" Write-Warn "Denk daran: Den Host '$agentHostname' im Zabbix-Frontend anlegen,"