ZabbixAgent2Installer/Public/Get-ZabbixAgentPackage.ps1
2025-09-18 03:03:12 +00:00

103 lines
No EOL
2.7 KiB
PowerShell

function Get-ZabbixAgentPackage {
<#
.Synopsis
Downloads Zabbix Agent Package
.DESCRIPTION
Downloads Zabbix Agent Package from a predefined URI for the Agent 2 from Zabbix CDN or your custom URI
The file is saved to the $env:TEMP because it's always writeable
Use Get-Help Get-ZabbixAgentPackage -Full to see the current hardcoded CDN URI
.EXAMPLE
# Just run with defaults and get a file object
$file = Get-ZabbixAgentPackage
$file
Directory: C:\Users\dalek5439587\AppData\Local\Temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 07.07.2023 13:34 13561856 ZabbixAgent2.msi
.EXAMPLE
# Specify some other URI and the resulting filename
$file = Get-ZabbixAgentPackage -SourceURI https://contoso.tld/zabbix_agent2.msi -FileName zabbixagent2-latest.msi
$file
Directory: C:\Users\dalek5439587\AppData\Local\Temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 07.07.2023 13:34 13561856 zabbixagent2-latest.msi
#>
[CmdletBinding()]
param (
# Source URI, so you can override it with a later version or your custom distribution point
$SourceURI = 'https://cdn.zabbix.com/zabbix/binaries/stable/7.0/latest/zabbix_agent2-7.0-latest-windows-amd64-openssl-static.zip',
# File name for the downloaded file
$FileName = 'ZabbixAgent2.zip'
)
function Enable-TransportLayerSecurity {
param (
$SecurityProtocol = [Net.SecurityProtocolType]::Tls12
)
# Get current one
$currentProtocol = [Net.ServicePointManager]::SecurityProtocol
# Stupid string comparison
if ($currentProtocol -notcontains $SecurityProtocol) {
# ADD the requested protocol to the current list
Write-Verbose "Enabling Tls12"
[Net.ServicePointManager]::SecurityProtocol = $currentProtocol -bor $SecurityProtocol
}
} # end Enable-TransportLayerSecurity
if ($SourceURI) {
Enable-TransportLayerSecurity
$outFile = Join-Path $env:TEMP $FileName
'Downloading agent from {0} to {1}' -f $SourceURI, $outFile | Write-Verbose
try {
$file = Invoke-WebRequest -Uri $SourceURI -OutFile $outFile
$file = Get-Item $outFile -ErrorAction Stop
$file
}
catch {
$err = @{
Exception = 'ZabbixAgentPackageDownloadFailed'
Message = 'Agent download failed, aborting: {0}' -f $Error[0].Exception
}
Write-Error @err
break
}
}
}