Deploy Zabbix agents to a large number of Windows-based servers using Powershell

Good afternoon!
Recently, I faced the task of quickly installing a large number of Zabbix agents on Windows servers.
I decided that this can and should be solved using scripts. As a “language" I chose my favorite Powershell (and not just me!).
You will find a small manual under the cut!

Powershell v2.0 + offers very convenient remote launch tools (Powershell Remoting) and I would love to use them, but in my case there was one problem - servers with Windows Server 2003 (without the necessary updates) could come across the "path", therefore I decided to use psexec.exe

I needed the most automated solution, so I wrote such a script in which I load the list of DNS names of servers from a file, and at the output I get an XML file for import into Zabbix.

The first thing we need to do is put the folder ( \\ server \ share \ DeployZabbix ) on the shared resource , in which there will be:
1) Agent file for x86-systems (zabbix_agentd86.exe)
2) Agent file for x64-systems (zabbix_agentd64 .exe)
3) The configuration file zabbix_agentd.conf (in which you need to specify at least the address / DNS name of the Zabbix server)
4) 2 batch files, Install86.bat and Install64.bat with the following contents:
Install86.bat:
"C:\Program Files\Zabbix\zabbix_agentd86.exe" --config "C:\Program Files\Zabbix\zabbix_agentd.conf" --install
net start "Zabbix Agent"


Install64.bat:
"C:\Program Files\Zabbix\zabbix_agentd64.exe" --config "C:\Program Files\Zabbix\zabbix_agentd.conf" --install
net start "Zabbix Agent"


The second thing we need is the folder in which the script for deploying the agents will lie, and everything necessary for its work:
1) Actually, the DeployZabbix.ps1 script itself (about it below)
2) Three .txt files (Source1.txt , Source2.txt, Source3.txt) - these are the “pieces” of the .xml file that will be output. Do not forget to replace the “Default” group with the one you need in the text!
Source1.txt
2.02012-11-21T09:18:17ZDefault



Source2.txt
0-12Default110



Source3.txt
10050if1


3) Actually, psexec.exe itself (you can get it from here )
4) The computers.csv file, in which the FQDN names of the servers go each from a new line.

Well, the third , most important, is the script itself. When writing a script, a bit test should be added. I also prefer to log actions into a text file, so that later, if something happens, you can see what happened wrong.

Remove-Item .\out.txt #уберем старые файлы
Remove-Item .\out.xml #уберем старые файлы
copy .\Source1.txt .\out.txt #скопируем первую часть xml в новый файл
#импортируем список компьютеров
Import-CSV ".\computers.csv" -header("ComputerName") | ForEach {
$ComputerName = $_.ComputerName #чуть-чуть упростим, мне так удобнее :)
New-Item "\\$ComputerName\c$\Program Files\Zabbix" -Type Directory
$path = "\\$ComputerName\c$\Program Files\Zabbix"
Copy-Item \\server\share\DeployZabbix\* $path
$bit = Get-WmiObject Win32_Processor -computername $ComputerName | where {$_.DeviceID -eq "CPU0"} | Select AddressWidth
if ($bit -like '*64*') {
.\psexec.exe \\$ComputerName "C:\\Program Files\Zabbix\Install64.bat" -h
}
else {
.\psexec.exe \\$ComputerName "C:\\Program Files\Zabbix\Install86.bat" -h
}
Add-Content .\out.txt ('')
Add-Content .\out.txt ('' + $_.ComputerName + '')
Add-Content .\out.txt ('' + $_.ComputerName + '')
gc Source2.txt | Out-File .\out.txt -Append -Encoding default
Add-Content .\out.txt ('' + $_.ComputerName + '')
gc Source3.txt | Out-File .\out.txt -Append -Encoding default
}
Add-Content .\out.txt ('')
Add-Content .\out.txt ('')
Rename-Item .\out.txt "out.xml"


You can also add lines so that the script removes unnecessary files, but because they weigh less than 1MB, I did not bother.

Then, the resulting .xml file (which will be in the directory with the script) is imported into the list of hosts in Zabbix.

That, in fact, is all. I hope this tutorial will be at least useful to someone.

Also popular now: