icinga-powershell-framework/lib/core/icingaagent/installer/Uninstall-IcingaAgent.psm1

52 lines
1.9 KiB
PowerShell
Raw Normal View History

2019-09-29 12:25:40 -04:00
function Uninstall-IcingaAgent()
{
param (
[switch]$RemoveDataFolder = $FALSE
);
$IcingaData = Get-IcingaAgentInstallation;
[string]$IcingaProgramData = Join-Path -Path $Env:ProgramData -ChildPath 'icinga2';
2019-09-29 12:25:40 -04:00
if ($IcingaData.Installed -eq $FALSE) {
Write-IcingaConsoleNotice 'Unable to uninstall the Icinga Agent. The Agent is not installed';
if ($RemoveDataFolder) {
if (Test-Path $IcingaProgramData) {
Write-IcingaConsoleNotice -Message 'Removing Icinga Agent directory: "{0}"' -Objects $IcingaProgramData;
return ((Remove-ItemSecure -Path $IcingaProgramData -Recurse -Force) -eq $FALSE);
} else {
Write-IcingaConsoleNotice -Message 'Icinga Agent directory "{0}" does not exist' -Objects $IcingaProgramData;
}
}
return $FALSE;
2019-09-29 12:25:40 -04:00
}
Stop-IcingaService -Service 'icinga2';
$Uninstaller = & powershell.exe -Command {
Use-Icinga -Minimal;
$IcingaData = $args[0];
$Uninstaller = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('{0} /q', $IcingaData.Uninstaller)) -FlushNewLine;
Start-Sleep -Seconds 2;
Optimize-IcingaForWindowsMemory;
return $Uninstaller;
} -Args $IcingaData;
2019-09-29 12:25:40 -04:00
if ($Uninstaller.ExitCode -ne 0) {
Write-IcingaConsoleError ([string]::Format('Failed to remove Icinga Agent: {0}{1}', $Uninstaller.Message, $Uninstaller.Error));
2019-09-29 12:25:40 -04:00
return $FALSE;
}
if ($RemoveDataFolder) {
Write-IcingaConsoleNotice -Message 'Removing Icinga Agent directory: "{0}"' -Objects $IcingaProgramData;
if ((Remove-ItemSecure -Path $IcingaProgramData -Recurse -Force) -eq $FALSE) {
return $FALSE;
}
}
Write-IcingaConsoleNotice 'Icinga Agent was successfully removed';
2019-09-29 12:25:40 -04:00
return $TRUE;
}