Skip to content

Instantly share code, notes, and snippets.

@bharkr
Last active May 29, 2024 17:54
Show Gist options
  • Save bharkr/a5eac7fe22f32f9fd22bee335039096b to your computer and use it in GitHub Desktop.
Save bharkr/a5eac7fe22f32f9fd22bee335039096b to your computer and use it in GitHub Desktop.
Install Zabbix Agent Windows

Install Zabbix Agent on Windows

Overview

This script installs the defined version of Zabbix Agent on Windows machines. It was designed to run via

Install-ZabbixAgent -ComputerName Server01 -zabbixurl https://cdn.zabbix.com/zabbix/binaries/stable/6.4/6.4.14/zabbix_agent-6.4.14-windows-amd64-openssl.msi

but could be used in a variety of workflows. It outputs a pscustomobject:

Name                           Value
----                           -----
TLSPSKIDENTITY                 PSK-Test-001-ZABB.contoso.com.psk
PreSharedKey                   0eaefcb7047fe1511ebf12038beaa44e
HostIPAddress                  123.123.123.123
HostName                       Test-001-ZABB

Pre-shared keys are created at runtime and unique per machine.

Parameter declaration:

  • $ComputerName = IP or hostname of device to run Zabbix Agent
  • $zabbixUrl = Link to the Zabbix Agent MSI you want to use (as of this writing, 6.4.3 is current)
function Install-ZabbixAgent {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$ComputerName,
[Parameter(Mandatory=$true)]
[string]$ZabbixUrl
)
if ($null -eq $Session) {
$Session = New-PSSession -ComputerName $ComputerName
}
function New-ZabbixPSK {
$i = 0
while ($i -le '3') {
$PSK += ((new-guid).guid).replace('-', '')
$i++
}
$PSK.Substring(14, 32)
}
$zabbixServer = '172.22.158.105'
Invoke-Command -Session $Session -ScriptBlock {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $Using:ZabbixUrl -OutFile "$env:TEMP\zabbix_agent.msi"
}
$PSK = New-ZabbixPSK
$logfile = "C:\ZabbixAgent-$($ComputerName)-$(Get-Date -Format yyyyMMddHH).log"
$installArgs = @(
"/qn"
"/L*v"
('"{0}"' -f $logfile)
"SERVER=$zabbixServer"
"SERVERACTIVE=$zabbixServer"
"HOSTNAME=$ComputerName"
"TLSCONNECT=psk"
"TLSACCEPT=psk"
"TLSPSKIDENTITY=PSK-$([System.Net.Dns]::GetHostByName($ComputerName).HostName).psk"
"TLSPSKVALUE=$PSK"
)
Invoke-Command -Session $Session -ScriptBlock {
Start-Process -Wait -FilePath "$env:TEMP\zabbix_agent.msi" -ArgumentList $Using:installArgs
}
$fwParams = @{
DisplayName = 'Allow Zabbix Checks on port 10050'
RemoteAddress = $zabbixServer
Direction = 'Inbound'
Protocol = 'TCP'
Action = 'Allow'
LocalPort = '10050'
Profile = 'Domain'
}
Invoke-Command -Session $Session -ScriptBlock {
New-NetFirewallRule @Using:fwParams | Out-Null
}
[PSCustomObject]$outputObj = @{
HostName = $ComputerName
PreSharedKey = $PSK
TLSPSKIDENTITY = "PSK-$([System.Net.Dns]::GetHostByName($ComputerName).HostName).psk"
HostIPAddress = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.IPAddress -notmatch '^(127|169\.254)' } | ForEach-Object { $_.IPAddress }) -join ', '
}
$outputObj
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment