From b4aa9f24cd721489c01729766a7dbdf9321d6952 Mon Sep 17 00:00:00 2001
From: Andreas Lindemark <andreas.lindemark@liu.se>
Date: Wed, 2 Apr 2025 18:36:20 +0200
Subject: [PATCH] Moved this script from another folder

---
 Windows/ZabbixAgent/RemoveZabbix.ps1 | 178 +++++++++++++++++++++++++++
 1 file changed, 178 insertions(+)
 create mode 100644 Windows/ZabbixAgent/RemoveZabbix.ps1

diff --git a/Windows/ZabbixAgent/RemoveZabbix.ps1 b/Windows/ZabbixAgent/RemoveZabbix.ps1
new file mode 100644
index 0000000..c05e2d3
--- /dev/null
+++ b/Windows/ZabbixAgent/RemoveZabbix.ps1
@@ -0,0 +1,178 @@
+<#=======================================================
+   Remove Zabbix Agent and Zabbix Agent 2 and clean up
+=======================================================#>
+
+# Start the Script
+Write-Host "Starting script execution..."
+
+# Determine the name of the currently executing script without the extension
+$scriptName = [System.IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name)
+
+# Set the log file path based on the script name
+$transcriptPath = "C:\Install Logs\$scriptName.log"
+
+# Start Transcript
+Start-Transcript -Path $transcriptPath
+
+# Initialize a flag to track if any uninstallation or removal has occurred
+$changesMade = $false
+
+# Function to check if a Chocolatey package is installed
+function Test-PackageInstalled {
+   param (
+      [string]$PackageName
+   )
+   $installedPackages = choco list --exact $PackageName
+   return $installedPackages -match "^$PackageName"
+}
+
+# Function to check if a service exists
+function Test-Service {
+   param (
+      [string]$ServiceName
+   )
+   return $null -ne (Get-Service -Name $ServiceName -ErrorAction SilentlyContinue)
+}
+
+# Function to stop a service if it exists
+function Stop-ServiceIfExists {
+   param (
+      [string]$ServiceName,
+      [ref]$changesMade
+   )
+   if (Test-Service -ServiceName $ServiceName) {
+      Write-Output "Stopping service: $ServiceName"
+      Stop-Service -Name $ServiceName -Force -ErrorAction SilentlyContinue
+      $changesMade.Value = $true
+   } else {
+      Write-Output "Service $ServiceName not found. No action taken."
+   }
+}
+
+# Function to uninstall a Chocolatey package if it exists
+function Uninstall-ChocoPackageIfExists {
+   param (
+      [string]$PackageName,
+      [ref]$changesMade
+   )
+   if (Test-PackageInstalled -PackageName $PackageName) {
+      Write-Output "Uninstalling package: $PackageName"
+      choco uninstall -y $PackageName
+      $changesMade.Value = $true
+   } else {
+      Write-Output "Package $PackageName not found. No action taken."
+   }
+}
+
+# Function to remove remaining files and registry entries
+function Remove-ZabbixAgentRemnants {
+   param (
+      [string]$AgentName,
+      [string]$InstallPath,
+      [string]$ServiceName,
+      [ref]$changesMade
+   )
+
+   # Stop and remove the service
+   Stop-ServiceIfExists -ServiceName $ServiceName -changesMade ([ref]$changesMade)
+   if (Test-Service -ServiceName $ServiceName) {
+      Write-Output "Removing service: $ServiceName"
+      sc.exe delete $ServiceName
+      $changesMade.Value = $true
+   }
+
+   # Remove installation directory
+   if (Test-Path -Path $InstallPath) {
+      Write-Output "Removing directory: $InstallPath"
+      Remove-Item -Path $InstallPath -Recurse -Force
+      $changesMade.Value = $true
+   }
+
+   # Remove registry entries
+   $registryPaths = @(
+      "HKLM:\SOFTWARE\$AgentName",
+      "HKLM:\SYSTEM\CurrentControlSet\Services\$ServiceName",
+      "HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\$AgentName"
+   )
+   foreach ($path in $registryPaths) {
+      if (Test-Path -Path $path) {
+         Write-Output "Removing registry path: $path"
+         Remove-Item -Path $path -Recurse -Force
+         $changesMade.Value = $true
+      }
+   }
+}
+
+# Function to search for Zabbix files in specified paths
+function Search-ZabbixFiles {
+   param (
+      [string]$searchPath,
+      [string]$searchPattern
+   )
+   Write-Output "Searching for Zabbix files in $searchPath..."
+   $files = Get-ChildItem -Path $searchPath -Recurse -Filter $searchPattern -ErrorAction SilentlyContinue
+   foreach ($file in $files) {
+      Write-Output "Found file: $($file.FullName)"
+   }
+}
+
+# Remove Zabbix Agent
+Write-Output "Stop Zabbix Agent service and uninstall."
+Stop-ServiceIfExists -ServiceName "Zabbix Agent" -changesMade ([ref]$changesMade)
+Uninstall-ChocoPackageIfExists -PackageName "zabbix-agent" -changesMade ([ref]$changesMade)
+Remove-ZabbixAgentRemnants -AgentName "Zabbix Agent" -InstallPath "C:\Program Files\Zabbix Agent" -ServiceName "Zabbix Agent" -changesMade ([ref]$changesMade)
+
+# Remove Zabbix Agent 2
+Write-Output "Stop Zabbix Agent 2 service and uninstall."
+Stop-ServiceIfExists -ServiceName "Zabbix Agent 2" -changesMade ([ref]$changesMade)
+Uninstall-ChocoPackageIfExists -PackageName "zabbix-agent2" -changesMade ([ref]$changesMade)
+Remove-ZabbixAgentRemnants -AgentName "Zabbix Agent 2" -InstallPath "C:\Program Files\Zabbix Agent 2" -ServiceName "Zabbix Agent 2" -changesMade ([ref]$changesMade)
+
+# Search for Zabbix files in specified paths
+Search-ZabbixFiles -searchPath "C:\ProgramData\chocolatey\.chocolatey" -searchPattern "*zabbix*"
+Search-ZabbixFiles -searchPath "C:\ProgramData\chocolatey" -searchPattern "*zabbix*"
+Search-ZabbixFiles -searchPath "C:\ProgramData\ChocolateyHttpCache" -searchPattern "*zabbix*"
+
+# Inform that a reboot should be done if any changes were made
+if ($changesMade) {
+   Write-Host ""
+   Write-Host "A reboot should be done before reinstalling any of the Zabbix agents." -ForegroundColor Yellow
+   Write-Host ""
+
+   # Windows GUI popup to remind about reboot
+   Add-Type -AssemblyName System.Windows.Forms
+   $popup = New-Object System.Windows.Forms.Form
+   $popup.Text = "Reboot Reminder"
+   $popup.Size = New-Object System.Drawing.Size(300,150)
+   $popup.StartPosition = "CenterScreen"
+
+   $label = New-Object System.Windows.Forms.Label
+   $label.Text = "A reboot should be done before reinstalling any of the Zabbix agents."
+   $label.AutoSize = $true
+   $label.Location = New-Object System.Drawing.Point(10,20)
+   $popup.Controls.Add($label)
+
+   $rebootButton = New-Object System.Windows.Forms.Button
+   $rebootButton.Text = "Reboot"
+   $rebootButton.Location = New-Object System.Drawing.Point(50,70)
+   $rebootButton.Add_Click({
+      Restart-Computer -Force
+   })
+   $popup.Controls.Add($rebootButton)
+
+   $cancelButton = New-Object System.Windows.Forms.Button
+   $cancelButton.Text = "Cancel"
+   $cancelButton.Location = New-Object System.Drawing.Point(150,70)
+   $cancelButton.Add_Click({
+      $popup.Close()
+   })
+   $popup.Controls.Add($cancelButton)
+
+   $popup.ShowDialog()
+}
+
+# Stop Transcript
+Write-Host "Stopping transcript..."
+Stop-Transcript
+
+Write-Host "Script execution completed."
\ No newline at end of file
-- 
GitLab