diff --git a/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 b/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 index eead0e032d8c98c5db584738b940dd74be747c28..a8d97966d4b3ae9d08823cc861a1fd473ca3891a 100644 --- a/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 +++ b/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 @@ -9,6 +9,7 @@ $agentConfigPath = "C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf" # Function to test if the Zabbix Agent 2 service is running function Test-ZabbixAgent2 { + Write-Output "Checking if Zabbix Agent 2 service is running..." $service = Get-Service -Name "Zabbix Agent 2" -ErrorAction SilentlyContinue if ($service -and $service.Status -eq 'Running') { Write-Output "Zabbix Agent 2 is running." @@ -19,16 +20,32 @@ function Test-ZabbixAgent2 { } } -# Function to copy the custom configuration file and start the service -function Start-ZabbixAgent2 { - Write-Output "Copying custom configuration file..." - Copy-Item -Path $customConfigPath -Destination $agentConfigPath -Force +# Function to copy the custom configuration file +function Copy-ZabbixConfig { + Write-Output "Copying custom configuration file from $customConfigPath to $agentConfigPath..." + try { + Copy-Item -Path $customConfigPath -Destination $agentConfigPath -Force + Write-Output "Configuration file copied successfully." + } catch { + Write-Output "Failed to copy configuration file: $_" + } +} +# Function to start the Zabbix Agent 2 service +function Start-ZabbixAgent2 { Write-Output "Starting Zabbix Agent 2 service..." - Start-Service -Name "Zabbix Agent 2" + try { + Start-Service -Name "Zabbix Agent 2" + Write-Output "Zabbix Agent 2 service started successfully." + } catch { + Write-Output "Failed to start Zabbix Agent 2 service: $_" + } } # Main script logic if (-not (Test-ZabbixAgent2)) { + Copy-ZabbixConfig Start-ZabbixAgent2 +} else { + Write-Output "Zabbix Agent 2 is already running. No action needed." } \ No newline at end of file