function Remove-ZabbixAgent { Write-Verbose 'Running Remove-ZabbixAgent' $state = [ordered]@{ Service = @() UninstallString = @() EventLogRegistration = $null } # Services $filter = "name like 'zabbix agent%'" $Service = @( gcim -ClassName Win32_Service -Filter $filter ) Write-Verbose -Message ('Found {0} services with the filter {1}' -f $service.Count, $filter) Foreach ($thisService in $Service) { # Default service strings: # "C:\Program Files\Zabbix Agent\zabbix_agentd.exe" --config "C:\Program Files\Zabbix Agent\zabbix_agentd.conf" # "C:\Program Files\Zabbix Agent 2\zabbix_agent2.exe" -c "C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf" -f=false $regex = '^"?(?\w\:\\.+(?zabbix_agent.?\.exe))"?' + '.+?' + '"?(?\w:\\.+\.conf)"?' if ($thisService.PathName -match $regex) { $state.Service += [pscustomobject][ordered]@{ ServiceName = $thisService.Name ExecutablePath = $Matches.exepath ConfigurationPath = $Matches.confpath ExecutableDir = Split-Path -Parent -Path $Matches.exepath } } } # System uninstall list $regUninstall = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall' $UninstallList = @( Get-ChildItem -Path $regUninstall | Get-ItemProperty | ? DisplayName -match 'Zabbix\s+?Agent' ) Write-Verbose -Message ('Found {0} uninstall strings' -f $UninstallList.Count) foreach ($thisItem in $UninstallList) { # why, why?! $UninstallString = ('{0} {1}' -f $thisItem.UninstallString, '/passive /norestart') -replace '\s?/I{',' /x{' $state.UninstallString += $UninstallString } # EventLog registration if ($gi = Get-Item 'HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\Zabbix Agent 2' -ErrorAction SilentlyContinue) { $State.EventLogRegistration = $gi } # stop services and call builtin service removal foreach ($thisService in $state.Service) { Stop-Service -Force -Name $thisService.ServiceName -Confirm:$false -Verbose # call builtin agent uninstall routine $splat = @{ FilePath = $thisService.ExecutablePath ArgumentList = '-c "{0}" -d' -f $thisService.ConfigurationPath } Write-Verbose ('Running built-in uninstall: {0} {1}' -f $splat.Filepath, $splat.ArgumentList) Start-Process -Wait @splat Start-Sleep -Seconds 1 # for some unknown reason (again) Z doesn't remove it's Service registration, so just in case: try { sc.exe delete $($thisService.ServiceName) } catch { # nothing, actually } } # call msiexec uninstaller foreach ($thisUninstallString in $state.UninstallString) { Write-Verbose ('Runing MSI uninstaller: {0}' -f $thisUninstallString) & (Get-command cmd.exe).Source -ArgumentList ('/c', $thisUninstallString) } # remove eventlog registration because sometimes it's left there and it blocks install if ($state.EventLogRegistration) { if (test-path $state.EventLogRegistration.PSPath -ErrorAction SilentlyContinue) { Write-Verbose ('Removing EventLogRegistration {0}' -f $state.EventLogRegistration.PSPath) Remove-Item $state.EventLogRegistration.PSPath -Force } } # remove the directory because... the installer sometimes refuse to install if it's not empty foreach ($thisService in $state.Service) { if (Test-Path $thisService.ExecutableDir) { Write-Verbose ('Removing Executable directory {0}' -f $thisService.ExecutableDir) Remove-Item -Path $thisService.ExecutableDir -Force -Recurse } } } # End Remove-ZabbixAgent