Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
CheckAndStartZabbixAgent2.ps1 1.73 KiB
# 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"

# Path to the 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
function Test-ZabbixAgent2 {
   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..."
   Write-Output " - Source: $customConfigPath"
   Write-Output " - Destination: $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..."
   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
$serviceRunning = Test-ZabbixAgent2
Write-Output "Service running status: $serviceRunning"
if (-not $serviceRunning) {
   Copy-ZabbixConfig
   Start-ZabbixAgent2
} else {
   Write-Output "Zabbix Agent 2 is already running. No action needed."
}