diff --git a/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 b/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 index 55098bfede2bcbd6bdbd1c596fdf5a5066e33219..ce6e608e1ecdb803c90d83128e1a6014d8663a1a 100644 --- a/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 +++ b/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 @@ -1,10 +1,15 @@ # Retrieve the domain name dynamically $domainName = (Get-CimInstance -ClassName Win32_ComputerSystem).Domain -# Path to the custom configuration file on SYSVOL using the dynamic domain name -$customConfigPath = "\\$domainName\SYSVOL\$domainName\Configs\zabbix_agent2.conf" +# Get computer name +$computerName = $env:COMPUTERNAME -# Path to the Zabbix Agent 2 configuration file +# Path to the configuration folder and files +$customConfigPath = "\\$domainName\SYSVOL\$domainName\Configs" +$customConfigFile = "$customConfigPath\zabbix_agent2_$computerName.conf" +$defaultConfigFile = "$customConfigPath\zabbix_agent2.conf" + +# Path to the local Zabbix Agent 2 configuration file $agentConfigPath = "C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf" # Function to test if the Zabbix Agent 2 service is running @@ -21,13 +26,13 @@ function Test-ZabbixAgent2 { } } -# Function to copy the custom configuration file +# Function to copy the configuration file function Copy-ZabbixConfig { - Write-Output "Copying custom configuration file..." - Write-Output " - Source: $customConfigPath" + Write-Output "Copying configuration file..." + Write-Output " - Source: $customConfigFile" Write-Output " - Destination: $agentConfigPath" try { - Copy-Item -Path $customConfigPath -Destination $agentConfigPath -Force + Copy-Item -Path $customConfigFile -Destination $agentConfigPath -Force Write-Output "Configuration file copied successfully." } catch { Write-Output "Failed to copy configuration file: $_" @@ -45,12 +50,55 @@ function Start-ZabbixAgent2 { } } +# Function to stop the Zabbix Agent 2 service +function Stop-ZabbixAgent2 { + Write-Output "Stopping Zabbix Agent 2 service..." + try { + Stop-Service -Name "Zabbix Agent 2" -Force + Write-Output "Zabbix Agent 2 service stopped successfully." + } catch { + Write-Output "Failed to stop Zabbix Agent 2 service: $_" + } +} + # Main script logic +# Check if custom config file exists, otherwise use default +if (-Not (Test-Path $customConfigFile)) { + if (-Not (Test-Path $defaultConfigFile)) { + Write-Output "Neither custom nor default configuration file exists." + # Check if the service is running + $serviceRunning = Test-ZabbixAgent2 + Write-Output "Service running status: $serviceRunning" + if ($serviceRunning) { + Write-Output "Exiting script." + exit + } else { + Start-ZabbixAgent2 + Write-Output "Exiting script." + exit + } + } else { + $customConfigFile = $defaultConfigFile + } +} + +# Check if Zabbix Agent 2 service is running $serviceRunning = Test-ZabbixAgent2 Write-Output "Service running status: $serviceRunning" -if (-not $serviceRunning) { + +if (-Not $serviceRunning) { + # Service is not running, copy config file and start service Copy-ZabbixConfig Start-ZabbixAgent2 } else { - Write-Output "Zabbix Agent 2 is already running. No action needed." + # Service is running, compare config files + if ((Get-Content $agentConfigPath -Raw) -eq (Get-Content $customConfigFile -Raw)) { + Write-Output "Configuration files are identical. No action needed." + } else { + Write-Output "Configuration files do not match. Updating configuration..." + # Files are different, stop service, copy config file, and start service + Stop-ZabbixAgent2 + Copy-ZabbixConfig + Start-ZabbixAgent2 + } } \ No newline at end of file