diff --git a/Windows/Chocolatey/Remove-Zabbix.ps1 b/Windows/Chocolatey/Remove-Zabbix.ps1 index f2d60fda7db7095b51e33a4921e1fd735f978ba7..3aa72cf9e25870794425f1ed56f23afabbf5500e 100644 --- a/Windows/Chocolatey/Remove-Zabbix.ps1 +++ b/Windows/Chocolatey/Remove-Zabbix.ps1 @@ -14,6 +14,9 @@ $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 ( @@ -34,11 +37,13 @@ function Test-Service { # Function to stop a service if it exists function Stop-ServiceIfExists { param ( - [string]$ServiceName + [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." } @@ -47,11 +52,13 @@ function Stop-ServiceIfExists { # Function to uninstall a Chocolatey package if it exists function Uninstall-ChocoPackageIfExists { param ( - [string]$PackageName + [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." } @@ -62,20 +69,23 @@ function Remove-ZabbixAgentRemnants { param ( [string]$AgentName, [string]$InstallPath, - [string]$ServiceName + [string]$ServiceName, + [ref]$changesMade ) # Stop and remove the service - Stop-ServiceIfExists -ServiceName $ServiceName + 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 @@ -88,6 +98,7 @@ function Remove-ZabbixAgentRemnants { if (Test-Path -Path $path) { Write-Output "Removing registry path: $path" Remove-Item -Path $path -Recurse -Force + $changesMade.Value = $true } } } @@ -107,22 +118,57 @@ function Search-ZabbixFiles { # Remove Zabbix Agent Write-Output "Stop Zabbix Agent service and uninstall." -Stop-ServiceIfExists -ServiceName "Zabbix Agent" -Uninstall-ChocoPackageIfExists -PackageName "zabbix-agent" -Remove-ZabbixAgentRemnants -AgentName "Zabbix Agent" -InstallPath "C:\Program Files\Zabbix Agent" -ServiceName "Zabbix Agent" +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" -Uninstall-ChocoPackageIfExists -PackageName "zabbix-agent2" -Remove-ZabbixAgentRemnants -AgentName "Zabbix Agent 2" -InstallPath "C:\Program Files\Zabbix Agent 2" -ServiceName "Zabbix Agent 2" +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" -searchPattern "*zabbix*" Search-ZabbixFiles -searchPath "C:\ProgramData\ChocolateyHttpCache" -searchPattern "*zabbix*" -# Inform that a reboot should be done -Write-Host "A reboot should be done before reinstalling any of the Zabbix agents." +# 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..."