Update-ZabbixAgentConfig.ps1

function Update-ZabbixAgentConfig
{
  <#
      .SYNOPSIS
      Updates a remote Zabbix Configuration File
      .DESCRIPTION
      Remotely updates a number of zabbix configuration files with certain parameters.
      .EXAMPLE
      Update-ZabbixAgentConfig -computer comp1 -paramToSet EnableRemoteCommands -valueToSet 1
      Sets EnableRemoteCommands=1 on comp1's Zabbix .conf file if it is found.
      .EXAMPLE
      Update-ZabbixAgentConfig -computer comp1,comp2 -paramToSet EnableRemoteCommands -valueToSet 0
      Sets EnableRemoteCommands=0 on the .conf file on comp1 and comp2
  #>

  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory = $false, Position = 0)]
    [Alias('Server')]
    [Object[]]
    $computer,
    
    [Parameter(Mandatory = $true, Position = 1)]
    [ValidateSet('DebugLevel','EnableRemoteCommands','Server','ServerActive','Hostname','Timeout',
        'Include','TLSConnect','TLSAccept','TLSCRLFile','TLSServerCertIssuer','TLSServerCertSubject',
      'TLSCertFile','TLSKeyFile','TLSPSKIdentity','TLSPSKFile')]
    [String]
    $paramToSet,
    
    [Parameter(Mandatory = $true, Position = 2)]
    [System.String]
    $valueToSet
  )
  
  foreach ($c in $computer) 
  {
    $confFound = $false
    $zabbixAgentFound = $false
    
    try {
      $configPath =  (Get-ZabbixAgent -Computer $c -ErrorAction Stop).confPath
      $zabbixAgentFound = $true
    }
    catch {
      write-error "unable to get zabbix agent. Ensure it is installed on $c."
      break
    }
    if ($zabbixAgentFound = $true) {
      $configPath = (($configPath.Replace(':','$')).insert(1,"\\$c\")).Replace('"','')
    
      if (Test-Path $configPath) {
        $confFound = $true
        $file = $configPath
      }

      <#
          #Alternative Method (deprecated) for finding .conf
 
          $possibleConfLocations =
          "\\$c\c$\zabbix\zabbix_agentd.win.conf",
          "\\$c\c$\Program Files\Zabbix Agent\zabbix_agentd.win.conf"
     
     
          foreach ($testLoc in $possibleConfLocations)
          {
          # Test All Locations for existing .conf filee
          if ($confFound)
          {
          # Do nothing if we've found a .conf
          }
          ELSE
          {
 
          if (Test-Path $testLoc)
          {
          # Conf file found!
          $confFound = $true
          Write-Output -InputObject "Conf file found at $testLoc"
          $file = $testLoc # Set location of .conf
          }
          }
          }
      #>

    
      if ($confFound) {
        # Conf Found. Can continue
    
    
        # Grab file Contents
        $cont = Get-Content $file
    
        #Set Variable for matched lines
        $matchedlines = 0
    
        $cont | Where-Object -FilterScript {
          if ($_ -match "^(()|( )|( )|( ))$paramToSet" -and $matchedlines -eq 0) 
          {
            $matchedlines++ # Increase counter of matched lines
            $lineIndex = $cont.IndexOf($_)
            $cont[$lineIndex] = "$paramToSet=$valueToSet" # Set new line content
          }
        }
    
        if ($matchedlines -eq 0) 
        {
          # We did not find a string matching $paramToSet. Add one.
          $cont | Where-Object -FilterScript {
            if ($_ -match "^(()|( )|( )|( ))(#)(()|( )|( )|( ))$paramToSet" -and $matchedlines -eq 0) 
            # Found the location of the default Commented string matching $paramToSet
            {
              $lineIndex = $cont.IndexOf($_)
              [System.Collections.ArrayList]$arrayCont = $cont # Create an Array list to be able to insert in position
              #$cont[$lineIndex++] = "$paramToSet=$valueToSet" # Set new line content
              $arrayCont.Insert(($lineIndex + 1),"$paramToSet=$valueToSet")
              $matchedlines++ # Increase counter of matched lines
              $cont = $arrayCont # Set our Content back to $cont
            }
          }        
        }
    
    
        Set-Content -Path $file -Value $cont   
      }
      ELSE {
        #No Conf Found
        Write-Error "No configuration file found for $c. Paths checked:
        $configPath"

      }    
    }
    else {
      Write-Output "Zabbix Agent not found on $c"
    }
  }

}