diff --git a/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 b/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 index a8d97966d4b3ae9d08823cc861a1fd473ca3891a..55098bfede2bcbd6bdbd1c596fdf5a5066e33219 100644 --- a/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 +++ b/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 @@ -9,20 +9,23 @@ $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." - return $true - } else { - Write-Output "Zabbix Agent 2 is not running." + try { + $service = Get-Service -Name "Zabbix Agent 2" -ErrorAction Stop + if ($service.Status -eq 'Running') { + return $true + } else { + return $false + } + } catch { return $false } } # Function to copy the custom configuration file function Copy-ZabbixConfig { - Write-Output "Copying custom configuration file from $customConfigPath to $agentConfigPath..." + Write-Output "Copying custom configuration file..." + Write-Output " - Source: $customConfigPath" + Write-Output " - Destination: $agentConfigPath" try { Copy-Item -Path $customConfigPath -Destination $agentConfigPath -Force Write-Output "Configuration file copied successfully." @@ -43,7 +46,9 @@ function Start-ZabbixAgent2 { } # Main script logic -if (-not (Test-ZabbixAgent2)) { +$serviceRunning = Test-ZabbixAgent2 +Write-Output "Service running status: $serviceRunning" +if (-not $serviceRunning) { Copy-ZabbixConfig Start-ZabbixAgent2 } else {