Update Deploy-ZabbixAgent2.ps1

Update SMB/HTTPS Failover
This commit is contained in:
2026-09-10 14:59:32 +10:00
parent 809e5d7e6b
commit b1bd830b0c

View File

@@ -78,29 +78,73 @@ $HardwareType = if ((Get-CimInstance -ClassName Win32_ComputerSystem).Model -mat
$MetadataVal = "Windows:$($HardwareType):$($Location):$($Role)"
# --- 4. File Pull from Local DC Share ---
$SourceZip = "$ShareSource\zabbix_agent2.zip"
$InstallDir = "C:\Program Files\Zabbix Agent 2"
$TempZip = "C:\Temp\zabbix_agent2.zip"
# --- 4. File Acquisition with Multi-Tier Fallback (SMB -> Git -> CDN) ---
$SourceZip = "$ShareSource\zabbix_agent2.zip"
$GitZipUrl = "https://git.mkau.au/DistractADD/zabbix/raw/branch/main/zabbix_agent2.zip"
$CdnZipUrl = "https://cdn.zabbix.com/zabbix/binaries/stable/7.4/7.4.11/zabbix_agent2-7.4.11-windows-amd64-openssl-static.zip"[cite: 1, 3, 4, 5]
$InstallDir = "C:\Program Files\Zabbix Agent 2"[cite: 1, 3, 4, 5]
$TempZip = "C:\Temp\zabbix_agent2.zip"[cite: 1, 3, 4, 5]
if (-not (Test-Path "C:\Temp")) { New-Item -ItemType Directory -Path "C:\Temp" -Force | Out-Null }
if (-not (Test-Path "C:\Temp")) { New-Item -ItemType Directory -Path "C:\Temp" -Force | Out-Null }[cite: 1, 3, 4, 5]
# Stop running instances safely before touching binaries
Get-Service "Zabbix Agent*" -ErrorAction SilentlyContinue | Stop-Service -Force -ErrorAction SilentlyContinue
Get-Process "zabbix_agent*" -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
# Stop running instances safely before replacing binaries
Get-Service "Zabbix Agent*" -ErrorAction SilentlyContinue | Stop-Service -Force -ErrorAction SilentlyContinue[cite: 1, 3, 4, 5]
Get-Process "zabbix_agent*" -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue[cite: 1, 3, 4, 5]
Copy-Item -Path $SourceZip -Destination $TempZip -Force
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12[cite: 1, 3, 4, 5]
$acquired = $false
# Attempt 1: Local DC SMB Share
try {
if (Test-Path $SourceZip -ErrorAction Stop) {
Copy-Item -Path $SourceZip -Destination $TempZip -Force -ErrorAction Stop
if ((Test-Path $TempZip) -and ((Get-Item $TempZip).Length -gt 1000000)) {
$acquired = $true
}
}
}
catch {
# SMB failed or blocked
}
# Attempt 2: Internal Git Server (Ideal for firewalled/isolated subnets)
if (-not $acquired) {
try {
Write-Host "SMB inaccessible. Downloading agent archive from internal Git..." -ForegroundColor Yellow
Invoke-WebRequest -Uri $GitZipUrl -OutFile $TempZip -UseBasicParsing -TimeoutSec 30 -ErrorAction Stop
if ((Test-Path $TempZip) -and ((Get-Item $TempZip).Length -gt 1000000)) {
$acquired = $true
}
}
catch {
# Git raw zip not present or timed out
}
}
# Attempt 3: Official Zabbix CDN Fallback
if (-not $acquired) {
try {
Write-Host "Git archive download failed. Falling back to official Zabbix CDN..." -ForegroundColor Yellow
Invoke-WebRequest -Uri $CdnZipUrl -OutFile $TempZip -UseBasicParsing -TimeoutSec 60 -ErrorAction Stop[cite: 1, 3, 4, 5]
if ((Test-Path $TempZip) -and ((Get-Item $TempZip).Length -gt 1000000)) {
$acquired = $true
}
}
catch {
throw "Critical: Failed to acquire Zabbix Agent archive via SMB, internal Git, or external CDN."
}
}
if (Test-Path $InstallDir) {
Remove-Item $InstallDir -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item $InstallDir -Recurse -Force -ErrorAction SilentlyContinue[cite: 1, 3, 4, 5]
}
New-Item -Path $InstallDir -ItemType Directory -Force | Out-Null
New-Item -Path $InstallDir -ItemType Directory -Force | Out-Null[cite: 1, 3, 4, 5]
Expand-Archive -Path $TempZip -DestinationPath $InstallDir -Force
Expand-Archive -Path $TempZip -DestinationPath $InstallDir -Force[cite: 1, 3, 4, 5]
$BinDir = Get-ChildItem -Path $InstallDir -Recurse | Where-Object { $_.Name -eq "zabbix_agent2.exe" } | Select-Object -ExpandProperty DirectoryName
$BinDir = Get-ChildItem -Path $InstallDir -Recurse | Where-Object { $_.Name -eq "zabbix_agent2.exe" } | Select-Object -ExpandProperty DirectoryName[cite: 1, 3, 4, 5]
if ($BinDir -ne $InstallDir) {
Copy-Item -Path "$BinDir\*" -Destination $InstallDir -Recurse -Force
Copy-Item -Path "$BinDir\*" -Destination $InstallDir -Recurse -Force[cite: 1, 3, 4, 5]
}
# --- 5. Generate Standardized Configuration ---