Install-RemoteZabbixAgent.ps1
function Install-RemoteZabbixAgent { <# .SYNOPSIS Installs and configures a remote Windows Zabbix agent .DESCRIPTION Installs and configures a remote Windows Zabbix agent .EXAMPLE Install-RemoteZabbixAgent -computer server1 -zabbixSource c:\zabbixagent\ -ZabbixAgentEnableRemoteCommands 1 -ZabbixAgentHostName server1 -StartService - Copies the zabbix source agent data from C:\zabbixagent\ to the remote computer at c:\zabbix - Installs the service - Configures the EnableRemoteCommands and Hostname fields - Starts the service. .EXAMPLE Install-RemoteZabbixAgent -computer server1 -zabbixSource '\\my.domain\programs\zabbix\' -ZabbixAgentDebugLevel 3 -ZabbixAgentEnableRemoteCommands 1 -ZabbixAgentServer zbx.my.domain -ZabbixAgentServerActive zbx.my.domain -ZabbixAgentHostname server1 -ZabbixAgentTimeout 30 -StartService #> [CmdletBinding()] param ( [Parameter(Mandatory = $true, Position = 0)] [System.Object[]] $computer, [Parameter(Mandatory = $true, Position = 1)] [System.String] $zabbixSource, [Parameter(Mandatory = $false, Position = 2)] [System.String] $ZabbixAgentDebugLevel, [Parameter(Mandatory = $false, Position = 3)] [System.String] $ZabbixAgentEnableRemoteCommands, [Parameter(Mandatory = $false, Position = 4)] [System.String] $ZabbixAgentServer, [Parameter(Mandatory = $false, Position = 5)] [System.String] $ZabbixAgentServerActive, [Parameter(Mandatory = $false, Position = 6)] [System.String] $ZabbixAgentHostname, [Parameter(Mandatory = $false, Position = 7)] [System.String] $ZabbixAgentTimeout, [Parameter(Mandatory = $false, Position = 8)] [System.String] $ZabbixAgentInclude, [Parameter(Mandatory = $false, Position = 9)] [System.String] $ZabbixAgentTLSConnect, [Parameter(Mandatory = $false, Position = 10)] [System.String] $ZabbixAgentTLSAccept, [Parameter(Mandatory = $false, Position = 11)] [System.String] $ZabbixAgentTLSCRLFile, [Parameter(Mandatory = $false, Position = 12)] [System.String] $ZabbixAgentTLSServerCertIssuer, [Parameter(Mandatory = $false, Position = 13)] [System.String] $ZabbixAgentTLSServerCertSubject, [Parameter(Mandatory = $false, Position = 14)] [System.String] $ZabbixAgentTLSCertFile, [Parameter(Mandatory = $false, Position = 15)] [System.String] $ZabbixAgentTLSKeyFile, [Parameter(Mandatory = $false, Position = 16)] [System.String] $ZabbixAgentTLSPSKIdentity, [Parameter(Mandatory = $false, Position = 17)] [System.String] $ZabbixAgentTLSPSKFile, [Parameter(Mandatory = $false, Position = 18)] [Switch] $StartService = $false ) foreach ($c in $computer) { $installSuccess = $false $alreadyExists = $false if ($( try { Get-ZabbixAgent -Computer $c -ErrorAction stop } catch { } ) ) { Write-Host -Object 'Found Zabbix Agent' } ELSE { Write-Host -Object "No Agent Found on $c" # Create C:\Zabbix on Remote Server if (!(Test-Path -Path "\\$c\c$\Zabbix\")) { New-Item -Path "\\$c\c$\Zabbix\" -ItemType Directory -Force } $zabbixDestination = Get-Item -Path "\\$c\c$\Zabbix\" # Copy Zabbix Source try { Copy-Item -Path $zabbixSource -Destination $zabbixDestination.Parent.FullName -Recurse -Force -ErrorAction Stop } catch { Write-Warning -Message "Ran into an error copying $_.Exception.Message" } # Install Zabbix Service try { Invoke-Command -ComputerName $c -ScriptBlock { c:\zabbix\zabbix_agentd.exe --install -c c:\zabbix\zabbix_agentd.win.conf } -Verbose -ErrorAction Stop } catch { $storage = $_.Exception.Message if ($_.Exception.Message -match 'installed successfully') { $installSuccess = $true } ELSEIF ($_.Exception.Message -match 'already exists') { $alreadyExists = $true } } if ($installSuccess) { Write-Output -InputObject "Successfully Installed Zabbix Agent on $c" } ELSEIF ($alreadyExists) { Write-Output -InputObject "Zabbix Agent Already Exists on $c" } $service = Get-Service -Name *zabbix* -ComputerName $c } # configure Zabbix Agent Write-Output -InputObject 'Beginning Customization Phase' #write-host $ZabbixAgentEnableRemoteCommands if ($ZabbixAgentDebugLevel.length -ne 0) { Write-Output -InputObject "Setting DebugLevel on $c" Update-ZabbixAgentConfig -computer $c -paramToSet DebugLevel -valueToSet $ZabbixAgentDebugLevel } if ($ZabbixAgentEnableRemoteCommands.length -ne 0) { Write-Output -InputObject "Setting EnableRemoteCommands on $c" Update-ZabbixAgentConfig -computer $c -paramToSet EnableRemoteCommands -valueToSet $ZabbixAgentEnableRemoteCommands } if ($ZabbixAgentServer.length -ne 0) { Write-Output -InputObject "Setting Server on $c" Update-ZabbixAgentConfig -computer $c -paramToSet Server -valueToSet $ZabbixAgentServer } if ($ZabbixAgentServerActive.length -ne 0) { Write-Output -InputObject "Setting ServerActive on $c" Update-ZabbixAgentConfig -computer $c -paramToSet ServerActive -valueToSet $ZabbixAgentServerActive } if ($ZabbixAgentHostname.length -ne 0) { Write-Output -InputObject "Setting Hostname on $c" Update-ZabbixAgentConfig -computer $c -paramToSet Hostname -valueToSet $ZabbixAgentHostname } if ($ZabbixAgentTimeout.length -ne 0) { Write-Output -InputObject "Setting Timeout on $c" Update-ZabbixAgentConfig -computer $c -paramToSet Timeout -valueToSet $ZabbixAgentTimeout } if ($ZabbixAgentInclude.length -ne 0) { Write-Output -InputObject "Setting Include on $c" Update-ZabbixAgentConfig -computer $c -paramToSet Include -valueToSet $ZabbixAgentInclude } if ($ZabbixAgentTLSConnect.length -ne 0) { Write-Output -InputObject "Setting TLSConnect on $c" Update-ZabbixAgentConfig -computer $c -paramToSet TLSConnect -valueToSet $ZabbixAgentTLSConnect } if ($ZabbixAgentTLSAccept.length -ne 0) { Write-Output -InputObject "Setting TLSAccept on $c" Update-ZabbixAgentConfig -computer $c -paramToSet TLSAccept -valueToSet $ZabbixAgentTLSAccept } if ($ZabbixAgentTLSCRLFile.length -ne 0) { Write-Output -InputObject "Setting TLSCRLFile on $c" Update-ZabbixAgentConfig -computer $c -paramToSet TLSCRLFile -valueToSet $ZabbixAgentTLSCRLFile } if ($ZabbixAgentTLSServerCertIssuer.length -ne 0) { Write-Output -InputObject "Setting TLSServerCertIssuer on $c" Update-ZabbixAgentConfig -computer $c -paramToSet TLSServerCertIssuer -valueToSet $ZabbixAgentTLSServerCertIssuer } if ($ZabbixAgentTLSServerCertSubject.length -ne 0) { Write-Output -InputObject "Setting TLSServerCertSubject on $c" Update-ZabbixAgentConfig -computer $c -paramToSet TLSServerCertSubject -valueToSet $ZabbixAgentTLSServerCertSubject } if ($ZabbixAgentTLSCertFile.length -ne 0) { Write-Output -InputObject "Setting TLSCertFile on $c" Update-ZabbixAgentConfig -computer $c -paramToSet TLSCertFile -valueToSet $ZabbixAgentTLSCertFile } if ($ZabbixAgentTLSKeyFile.length -ne 0) { Write-Output -InputObject "Setting TLSKeyFile on $c" Update-ZabbixAgentConfig -computer $c -paramToSet TLSKeyFile -valueToSet $ZabbixAgentTLSKeyFile } if ($ZabbixAgentTLSPSKIdentity.length -ne 0) { Write-Output -InputObject "Setting TLSPSKIdentity on $c" Update-ZabbixAgentConfig -computer $c -paramToSet TLSPSKIdentity -valueToSet $ZabbixAgentTLSPSKIdentity } if ($ZabbixAgentTLSPSKFile.length -ne 0) { Write-Output -InputObject "Setting TLSPSKFile on $c" Update-ZabbixAgentConfig -computer $c -paramToSet TLSPSKFile -valueToSet $ZabbixAgentTLSPSKFile } if ($StartService) {Get-Service *zabbix* -ComputerName $c | Start-Service } } } |