Skip to content
Snippets Groups Projects
Commit 2c1f9fcd authored by Andreas Lindemark's avatar Andreas Lindemark
Browse files

Added support for custom config files.

Added check to compare source and target files
Added code to handle if any source files is missing (for example if the network is down) to start service if not running.
parent 5934c65b
Branches
No related tags found
No related merge requests found
# Retrieve the domain name dynamically # Retrieve the domain name dynamically
$domainName = (Get-CimInstance -ClassName Win32_ComputerSystem).Domain $domainName = (Get-CimInstance -ClassName Win32_ComputerSystem).Domain
# Path to the custom configuration file on SYSVOL using the dynamic domain name # Get computer name
$customConfigPath = "\\$domainName\SYSVOL\$domainName\Configs\zabbix_agent2.conf" $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" $agentConfigPath = "C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf"
# Function to test if the Zabbix Agent 2 service is running # Function to test if the Zabbix Agent 2 service is running
...@@ -21,13 +26,13 @@ function Test-ZabbixAgent2 { ...@@ -21,13 +26,13 @@ function Test-ZabbixAgent2 {
} }
} }
# Function to copy the custom configuration file # Function to copy the configuration file
function Copy-ZabbixConfig { function Copy-ZabbixConfig {
Write-Output "Copying custom configuration file..." Write-Output "Copying configuration file..."
Write-Output " - Source: $customConfigPath" Write-Output " - Source: $customConfigFile"
Write-Output " - Destination: $agentConfigPath" Write-Output " - Destination: $agentConfigPath"
try { try {
Copy-Item -Path $customConfigPath -Destination $agentConfigPath -Force Copy-Item -Path $customConfigFile -Destination $agentConfigPath -Force
Write-Output "Configuration file copied successfully." Write-Output "Configuration file copied successfully."
} catch { } catch {
Write-Output "Failed to copy configuration file: $_" Write-Output "Failed to copy configuration file: $_"
...@@ -45,12 +50,55 @@ function Start-ZabbixAgent2 { ...@@ -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 # 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 $serviceRunning = Test-ZabbixAgent2
Write-Output "Service running status: $serviceRunning" Write-Output "Service running status: $serviceRunning"
if (-not $serviceRunning) {
if (-Not $serviceRunning) {
# Service is not running, copy config file and start service
Copy-ZabbixConfig Copy-ZabbixConfig
Start-ZabbixAgent2 Start-ZabbixAgent2
} else { } 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment