From 73976fc83d7e24c15b5c994da5266ad41f9c3852 Mon Sep 17 00:00:00 2001 From: Andreas Lindemark <andreas.lindemark@liu.se> Date: Wed, 2 Apr 2025 20:49:12 +0200 Subject: [PATCH] Finally got this script to work as I wanted it to work --- .../ZabbixAgent/CheckAndStartZabbixAgent2.ps1 | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 b/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 index a8d9796..55098bf 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 { -- GitLab