Skip to content

Instantly share code, notes, and snippets.

@Santaro255
Last active May 28, 2024 18:24
Show Gist options
  • Save Santaro255/05c6f14c384f91f604523c1ab8cd754b to your computer and use it in GitHub Desktop.
Save Santaro255/05c6f14c384f91f604523c1ab8cd754b to your computer and use it in GitHub Desktop.
$os_name = (Get-WmiObject -Class Win32_OperatingSystem).CSName
$os_ver = (Get-WmiObject -Class Win32_OperatingSystem).OSArchitecture
#1 - PC, 2 - DC, 3 - server
#$os_type = (Get-WmiObject -Class Win32_OperatingSystem).ProductType
### Settings ###
$agents_store = "\\srv\folder\zabbix_agents"
$zabbix_server = "IP ZABBIX Server"
$install_log="\\srv\folder\zabbix_agents\install_logs\$os_name.log"
#################
Add-Content -Path $install_log -Value "$(Get-Date)"
Add-Content -Path $install_log -Value "$os_name"
try { New-Item -ItemType Directory -Path "C:\zabbix" -ErrorAction Stop | Out-Null
switch ($ver) {
{$os_ver -like "32*"} { Copy-Item -Path "$agents_store\x86\bin\zabbix_agentd.exe" -Destination "C:\zabbix" -Recurse
Add-Content -Path $install_log -Value "Zabbix Agent x86" }
{$os_ver -like "64*"} { Copy-Item -Path "$agents_store\x64\bin\zabbix_agentd.exe" -Destination "C:\zabbix" -Recurse
Add-Content -Path $install_log -Value "Zabbix Agent x64"}}
}
catch [System.Management.Automation.ActionPreferenceStopException] {
Add-Content -Path $install_log -Value $Error[0]
}
try { New-Item -ItemType File -Path "C:\zabbix\zabbix_agentd.conf" -ErrorAction Stop | Out-Null
Add-Content -Path "C:\zabbix\zabbix_agentd.conf" -Value "Hostname=$os_name"
Add-Content -Path "C:\zabbix\zabbix_agentd.conf" -Value "Server=$zabbix_server"
Add-Content -Path "C:\zabbix\zabbix_agentd.conf" -Value "ServerActive=$zabbix_server"
Add-Content -Path "C:\zabbix\zabbix_agentd.conf" -Value "HostMetadataItem=system.uname"
Add-Content -Path "C:\zabbix\zabbix_agentd.conf" -Value "EnableRemoteCommands=1"
Add-Content -Path "C:\zabbix\zabbix_agentd.conf" -Value "LogRemoteCommands=1"
Add-Content -Path "C:\zabbix\zabbix_agentd.conf" -Value "LogFile=C:\zabbix\zabbix_agentd.log"
Add-Content -Path $install_log -Value "Создан файл конфигурации для сервера $zabbix_server"
}
catch [System.Management.Automation.ActionPreferenceStopException] {
Add-Content -Path $install_log -Value $Error[0]
}
switch ($q_exist=!(Get-Service -Name 'Zabbix Agent' -ErrorAction SilentlyContinue)) {
{$q_exist -like "True"} { Start-Process -WorkingDirectory "$env:windir" -FilePath "cmd.exe" -ArgumentList "/c c:\zabbix\zabbix_agentd.exe -c c:\zabbix\zabbix_agentd.conf -i" -Verb runas
Add-Content -Path $install_log -Value "Zabbix Agent installed." }
{$q_exist -like "False"} { Add-Content -Path $install_log -Value "Service Zabbix agent already installed." }
}
Start-Sleep 5
switch ($q_status=(Get-Service -Name "Zabbix Agent").Status) {
{$q_status -eq "Running"} { Add-Content -Path $install_log -Value "Serivce Zabbix Agent already is running." }
{$q_status -eq "Stopped"} { Start-Process -WorkingDirectory "$PSHOME" -FilePath "powershell.exe" -ArgumentList "Start-Service -Name 'Zabbix Agent'" -Verb runas
Add-Content -Path $install_log -Value "Zabbix Agent Service is started." }
}
switch ($f_rule=!(netsh advfirewall firewall show rule name="All" | findstr "zabbix_agent_passive_in")) {
{$f_rule -like "True"} { Start-Process -WorkingDirectory "$env:windir" -FilePath "cmd.exe" -ArgumentList '/c netsh advfirewall firewall add rule name="zabbix_agent_passive_in" dir=in protocol=TCP profile=Domain,Private localport=10050 action=allow enable=yes' -Verb runas
Add-Content -Path $install_log -Value "Add firewall rule zabbix_agent_passive_in." }
{$f_rule -like "False"} { Add-Content -Path $install_log -Value "Firewall rule zabbix_agent_passive_in already exist." }
}
Add-Content -Path $install_log -Value "###config###"
Add-Content -Path $install_log -Value "$(Get-Content "C:\zabbix\zabbix_agentd.conf")"
Add-Content -Path $install_log -Value "############"
Start-Sleep 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment