Ad Widget

Collapse

Installation via Powershell .msi : Agent Zabbix Windows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sembitvea
    Junior Member
    • Mar 2023
    • 1

    #1

    Installation via Powershell .msi : Agent Zabbix Windows

    ​Hello tout le monde, j'écris un script afin d'installer un agent Zabbix v2 sur plusieurs serveurs. Pour l'installation, j'utilise donc la commande suivante :
    Code:
    Start-Process -FilePath msiexec.exe -ArgumentList "/i zabbix_agent2-6.4.0-windows-amd64-openssl.msi SERVER=<AdressIp> SERVERACTIVE=<AdressIp> /qn /l*v log.txt" -Wait
    Malheureusement le prompt revient au bout de 3sc et rien ne s'installe. Lorsque je retire l'argument
    Code:
    /qn
    j'ai bien la fenêtre d'installation qui se lance et si je vais plus loin je retrouve mes arguments
    Code:
    SERVER=<AdressIp> SERVERACTIVE=<AdressIp>
    bien remplis. Une idée ? Merci !​
  • krzysztof.tech
    Junior Member
    • Mar 2023
    • 8

    #2
    Hello, I'm about to leave the office and head home, so I quickly share my script with you. Remember to allow script execution on your computer using the PowerShell command from an admin account.

    "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUse"

    My script downloads a client that is compatible with the 32/64-bit version of the system. After installation, you'll have the choice to name the host, server address, and metadata entry. Finally, I added the generation of PS1 files for the scripts I use.

    This script installs the Zabbix agent 2 for new hosts and is only a small part of my work.

    By default, I have a cmd script with options to uninstall the old installation, install on a new host, force a shutdown if there is an error, and update the scripts' host and server names. Everything starts from one start.cmd file, which generates the rest of the files, elevates permissions, and does the job. The installation takes less than a minute.

    Check out my solution below, and if you have any problems, let me know. Although my English is terrible, I use a translator, but I think the comments in the code will make it easier for you.


    Code:
    #Create ZABBIX working directory
    Clear-Host
    Write-Host "Creating a ZABBIX working directory"
    Start-Sleep -Seconds 2
    New-Item -ItemType Directory -Path "$env:SystemDrive\ZABBIX" -Force | Out-Null
    
    #Get the current working directory
    Write-Host "Download Current Client"
    Start-Sleep -Seconds 2
    $CURPATH = Split-Path -Path $MyInvocation.MyCommand.Path -Parent
    $SOURCE = $CURPATH
    #ZABBIX Installation
    #Install ZABBIX %VER% version
    
    #Downloading the correct ZABBIX version for the system architecture
    if ($env:PROCESSOR_ARCHITECTURE -eq "AMD64") {
    #Downloading the correct ZABBIX version for the system architecture
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Invoke-WebRequest -Uri "https://cdn.zabbix.com/zabbix/binaries/stable/6.2/6.2.7/zabbix_agent2-6.2.7-windows-amd64-openssl.msi"  -OutFile "$env:SystemDrive\ZABBIX\zabbix_agent2-6.2.7-windows-amd64-openssl.msi"
    } else {
    # Version for 32-bit architecture
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Invoke-WebRequest -Uri "https://cdn.zabbix.com/zabbix/binaries/stable/6.2/6.2.7/zabbix_agent2-6.2.7-windows-i386-openssl.msi" -OutFile "$env:SystemDrive\ZABBIX\zabbix_agent2-6.2.7-windows-i386-openssl.msi"
    }
    #Install the downloaded version of ZABBIX for the appropriate system architecture
    if ($env:PROCESSOR_ARCHITECTURE -eq "AMD64") {
    # Installation of the version for 64-bit architecture
    Start-Process -FilePath "$env:SystemDrive\ZABBIX\zabbix_agent2-6.2.7-windows-amd64-openssl.msi" -ArgumentList "/qn SERVER=127.0.0.1 SERVERACTIVE=127.0.0.1 HOSTNAME=OMEN-host" -Wait
    } else {
    # Installation of the version for 32-bit architecture
    Start-Process -FilePath "$env:SystemDrive\ZABBIX\zabbix_agent2-6.2.7-windows-i386-openssl.msi" -ArgumentList "/qn SERVER=127.0.0.1 SERVERACTIVE=127.0.0.1 HOSTNAME=OMEN-host" -Wait
    }
    #The 32-bit version has been downloaded and installed when the architecture is not "AMD64".
    
    Start-Sleep -Seconds 3
    Write-Host "Client installed"
    Start-Sleep -Seconds 2
    
    $OutputEncoding = [System.Console]::InputEncoding = [System.Console]::OutputEncoding = [System.Text.Encoding]::Unicode
    
    
    # Get the path to the configuration file
    Write-Host "Get the path to the configuration file"
    $confFile = "C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf"
    
    # Changing the server address in the file
    $options = @{
        "1" = @{
            "Option" = "1 "
            "Value" = "serwer-vzabbix.net"
        }
        "2" = @{
            "Option" = "2 "
            "Value" = "serwer2-vzabbix.com"
        }
    }
    
    
    Clear-Host
    Write-Host "Select a value for Server:"
    $options.GetEnumerator() | Sort-Object Key | ForEach-Object {
        Write-Host "$($_.Value.Option): $($_.Value.Value)"
    }
    
    $selectedOption = Read-Host "Choose an option"
    
    if ($options.ContainsKey($selectedOption)) {
        $metadataItemValue = $options[$selectedOption]["Value"]
        $replacedServer = $false
        $replacedServerActive = $false
        (Get-Content $confFile) | ForEach-Object {
            if ($_ -match "^\s*(Server\s*=).*") {
                # Update Server option in line containing Server, preceded by a space,
                # with or without leading "#" and with or without spaces around "="
                $_ -replace "^\s*(#*\s*Server\s*=).*", "`$1$metadataItemValue"
                $replacedServer = $true
            } elseif
            ($_ -match "^\s*(ServerActive\s*=).*") {
                # Update Server option in line containing Server, preceded by a space,
                # with or without leading "#" and with or without spaces around "="
                $_ -replace "^\s*(#*\s*ServerActive\s*=).*", "`$1$metadataItemValue"
                $replacedServer = $true
            } else {
                # Preserve other lines
                $_
            }
        } | Set-Content $confFile
        if ($replacedServer -or $replacedServerActive) {
            Write-Host "Server and ServerActive have been changed to $metadataItemValue"
        } else {
            Write-Host "The Server and ServerActive options were not found in the configuration file."
        }
    } else {
        Write-Host "Invalid selection option."
    }
    
    # Reading the contents of the file into a variable
    $ConfigContent = Get-Content $confFile
    # Find the line containing the hostname and display its current value
    $CurrentHostnameLine = $ConfigContent | Where-Object { $_ -match "^Hostname=" }
    $CurrentHostname = $CurrentHostnameLine -replace "^Hostname=", ""
    Write-Host "Current hostname: $CurrentHostname"
    # Prośba o podanie nowej nazwy hosta
    $NewHostname = Read-Host "Enter a new hostname"
    # Zmiana nazwy hosta w pliku
    $NewHostnameLine = "Hostname=$NewHostname"
    $ConfigContent = $ConfigContent -replace "^Hostname=.*$", $NewHostnameLine
    Set-Content $confFile -Value $ConfigContent
    Write-Host "The hostname has been changed to: $NewHostname"
    
    $options = @{
        "1" = @{
            "Option" = "1 SQL"
            "Value" = "WIN,SQL"
        }
        "2" = @{
            "Option" = "2 OFFICE"
            "Value" = "WIN,OFFICE"
        }
        "3" = @{
            "Option" = "3 WAREHOUSE"
            "Value" = "WIN,WAREHOUSE"
        }
        "4" = @{
            "Option" = "4 SALE"
            "Value" = "WIN,SALE"
        }
          "5" = @{
            "Option" = "5 CASH"
            "Value" = "WIN,CASH"
        }
          "6" = @{
            "Option" = "6 LAPTOP"
            "Value" = "WIN,LAPTOP"
        }
    }
    
    Clear-Host
    Write-Host "Select a value for HostMetadataItem:"
    $options.GetEnumerator() | Sort-Object Key | ForEach-Object {
        Write-Host "$($_.Value.Option): $($_.Value.Value)"
    }
    
    $selectedOption = Read-Host "Choose an option"
    
    if ($options.ContainsKey($selectedOption)) {
        $metadataItemValue = $options[$selectedOption]["Value"]
        $replacedHostMetadataItem = $false
        $replacedHostMetadata = $false
        (Get-Content $confFile) | ForEach-Object {
            if ($_ -match "^\s*#*\s*HostMetadataItem\s*=.*") {
                # Update HostMetadataItem option in line containing HostMetadataItem,
                # with or without leading "#" and with or without spaces around "="
                $_ -replace "^\s*#*\s*(HostMetadataItem\s*=).*", "`$1$metadataItemValue"
                $replacedHostMetadataItem = $true
            } elseif ($_ -match "^\s*#*\s*HostMetadata\s*=.*") {
                # Update HostMetadata option in line containing HostMetadata,
                # with or without leading "#" and with or without spaces around "="
                $_ -replace "^\s*#*\s*(HostMetadata\s*=).*", "`$1$metadataItemValue"
                $replacedHostMetadata = $true
            } else {
                # Preserve other lines
                $_
            }
        } | Set-Content $confFile
        if ($replacedHostMetadataItem -or $replacedHostMetadata) {
            Write-Host "Value HostMetadataItem i HostMetadata C $metadataItemValue."
        } else {
            Write-Host "The HostMetadataItem and HostMetadata options were not found in the configuration file."
        }
    } else {
        Write-Host "Invalid selection option."
    }
    # Change temp for ps1 file files
    (Get-Content -Path "C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf") | ForEach-Object { $_ -replace "Include=.\\", "# Include=.\" } | Set-Content -Path "C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf"
    # Change Timeout for scripts to maximum time
    (Get-Content -Path "C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf") | ForEach-Object { $_ -replace "# Timeout=3", "Timeout=30" } | Set-Content -Path "C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf"
    
    Start-Sleep -Seconds 2
    # Change UserParameter= for PS1 files
    $filePath = "C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf"
    $newLines = @"
    UserParameter=PowerShell1,powershell.exe -NoProfile -ExecutionPolicy bypass -file "C:\Program Files\Zabbix Agent 2\1.ps1"
    UserParameter=PowerShell2,powershell.exe -NoProfile -ExecutionPolicy bypass -file "C:\Program Files\Zabbix Agent 2\2.ps1"
    UserParameter=PowerShell3,powershell.exe -NoProfile -ExecutionPolicy bypass -file "C:\Program Files\Zabbix Agent 2\3.ps1"
    UserParameter=PowerShell4,powershell.exe -NoProfile -ExecutionPolicy bypass -file "C:\Program Files\Zabbix Agent 2\4.ps1"
    UserParameter=PowerShell5,powershell.exe -NoProfile -ExecutionPolicy bypass -file "C:\Program Files\Zabbix Agent 2\5.ps1"
    UserParameter=PowerShell6,powershell.exe -NoProfile -ExecutionPolicy bypass -file "C:\Program Files\Zabbix Agent 2\6.ps1"
    "@
    Add-Content -Path $filePath -Value $newLines
    
    
    #Creating a powershell script 1.ps1
    Start-Sleep -Seconds 1
    Set-Content -Path "C:\Program Files\Zabbix Agent 2\1.ps1" -Value @'
    
    insert powershell script content here 1.ps1
    
    '@
    
    Start-Sleep -Seconds 1
    #Creating a powershell script 2.ps1
    
    Set-Content -Path "C:\Program Files\Zabbix Agent 2\2.ps1" -Value @'
    
    insert powershell script content here 2.ps1
    
    '@
    Start-Sleep -Seconds 1
    #Creating a powershell script 3.ps1
    
    Set-Content -Path "C:\Program Files\Zabbix Agent 2\3.ps1" -Value @'
    
    insert powershell script content here 3.ps1
    
    '@
    Start-Sleep -Seconds 1
    #Creating a powershell script 4.ps1
    
    Set-Content -Path "C:\Program Files\Zabbix Agent 2\4.ps1" -Value @'
    
    insert powershell script content here 4.ps1
    
    '@
    
    Start-Sleep -Seconds 1
    #Creating a powershell script 5.ps1
    
    Set-Content -Path "C:\Program Files\Zabbix Agent 2\5.ps1" -Value @'
    insert powershell script content here 4.ps1
    '@
    
    #Creating a powershell powershell 6.ps1
    
    Set-Content -Path "C:\Program Files\Zabbix Agent 2\6.ps1" -Value @'
    
    insert powershell script content here 6.ps1
    '@
    
    ### end of script generation section
    Start-Sleep -Seconds 1
    
    # Service reset
    Restart-Service -Name "Zabbix Agent 2"
    Start-Sleep -Seconds 1
    Write-Host "Zabbix Agent 2 service restart done"
    Start-Sleep -Seconds 2
    Remove-Item $env:SystemDrive\ZABBIX -Recurse -Force
    exit
    
    ### koniec sekcji generownia skryptow
    Start-Sleep -Seconds 1
    
    Start-Sleep -Seconds 1
    
    # Service reset
    Restart-Service -Name "Zabbix Agent 2"
    Start-Sleep -Seconds 1
    Write-Host "Restar usługi Zabbix Agent 2 wykonany"
    Start-Sleep -Seconds 2
    Remove-Item $env:SystemDrive\ZABBIX -Recurse -Force
    exit​​

    Comment

    Working...