I’ve made a script to update all the Zabbix agents on Windows servers in a domain. The script does the following:

  1. Qeries Active Directory and searches for all Windows Servers in a Domain.
  2. Tests for each remote server queried by Active Directory if the specified folder. exists on the defined path.
  3. If the path exists it will stop the defined service.
  4. It will then rename the specified file on the target directory as a backup.
  5. Next it will copy all files from the repository location to the target path.
  6. Finally, it starts the service again when the files have finished copying.

For this script to function properly, you need the following:

  1. Windows PowerShell Version 3 on the server that will execute the script
  2. The Active Directory module for Windows PowerShell installed on the server that will execute the script.
  3. The Zabbix Agent source files downloaded and stored on a central location or fileshare that all servers can access using the credentials of the account that executes the script. This should be an administrator account. You can download the latest Zabbix agent for Windows here.

Important: You should only replace the executable files in the agent folder, thus only put these files in the source location. The files are zabbix_agent.exe, zabbix_get.exe and zabbix_sender.exe.
(You do not want to replace your existing .conf file with the default .conf file)

Without further ado, here’s the script:

#requires -version 3

#Update-ZabbixAgent.ps1

<#
  ****************************************************************
  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *
  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *
  ****************************************************************
#>

<#
.Synopsis
Update-ZabbixAgent.ps1 updates zabbix agents on multiple Windows remote machines in a domain using a shared repository
.Description

Update-ZabbixAgent.ps1 can be used to update multiple Zabbix agents in a Windows domain

DETAILS

The script does the following:
1. queries Active Directory and searches for all Windows Servers in a Domain.
2. tests for each remote server queried by Active Directory if the specified folder exists on the defined path
3. if the path exists it will stop the defined service 
4. it will then rename the specified file on the target directory as a backup
5. next it will copy all files from the repository location to the target path
6. finally, it starts the service again when the files have finished copying

Requirements:
Windows PowerShell Version 3 on the server that will execute the script
The Active Directory module for Windows PowerShell installed on the server that will execute the script.
The Zabbix Agent source files downloaded and stored on a central location or fileshare that all servers can 
access using the credentials of the account that executes the script. This should be an administrator account. 
You can download the latest Zabbix agent for Windows from this URI: http://www.zabbix.com/download.php

.EXAMPLE
.\Update-ZabbixAgent -verbose

This will run the command and output verbose information for troubleshooting

.NOTES
v1.3
Last Updated 10-7-2015
Marco Janse (https://ictstuff.info)

#>

[cmdletbinding()]
param()

#Modules
Import-Module ActiveDirectory -ErrorAction SilentlyContinue

#Adjustable Variables
#(not all of these variables might have been used, but I like to have them here for future modifications of this script)
$server = Read-Host "Enter the NETBIOS name of the server that holds the source files"
$today = Get-Date
$date = $today.ToString("yyyy-MM-dd")
$domains = (Get-ADForest).Domains
$service = "Zabbix Agent"
$folder = "Zabbix"
$file = "zabbix_agentd.conf"
$source = "\\$server\c$\install\$folder"

#Get all servers in the current Domain
$Computers = Get-ADComputer -Filter { OperatingSystem -like "Windows*Server*" } | Select-Object -ExpandProperty DNSHostName
Write-Verbose "Found the following Servers: $Computers"

#script execution
ForEach ($computer in $computers) { 
       If((Test-Path "\\$Computer\c$\$folder" -Verbose) -eq "True") {
                            Write-Verbose "path found for $computer"
                            Get-Service -Name $service -ComputerName $Computer| Stop-Service -ErrorAction Inquire -Verbose
                            Start-Sleep -Seconds 5
                            Rename-Item -Path \\$computer\C$\$folder\$file -NewName $file-$date -ErrorAction Inquire -Verbose
                            Copy-Item -Path $source -Recurse -Destination \\$computer\c$\ -Force -ErrorAction Inquire -Verbose
                            Get-Service -Name $service -ComputerName $computer | Start-Service -ErrorAction SilentlyContinue -Verbose
       } Else { 
                            Write-Host "Path not found for $computer"
       }
}
#End of script

 

  1. That’s all! Just assure that your windows host Status is set to Enabled and wait a few minutes in order for Zabbix server to contact the windows agent side and process the received remote data.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>