Skip to content

Commit 7d3ac7e

Browse files
authored
Merge pull request #211 from Icinga:feature/add_support_to_uninstall_components
Feature: Adds support to uninstall Icinga for Windows Adds feature to uninstall single components for Icinga for Windows or to uninstall everything and start entirely from new.
2 parents 96afdc7 + 1638465 commit 7d3ac7e

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

doc/31-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
2020
* [#205](https://github.com/Icinga/icinga-powershell-framework/pull/205) Ensure Icinga for Windows configuration file is opened as read-only for every single task besides actually modifying configuration content
2121
* [#207](https://github.com/Icinga/icinga-powershell-framework/pull/207) Adds new Argument `-LabelName` to `New-IcingaCheck`, allowing the developer to provide custom label names for checks and override the default based on the check name.
2222
* [#210](https://github.com/Icinga/icinga-powershell-framework/pull/210) Updates the Icinga DSL for building PowerShell arrays to ensure all string values are properly escaped with `'`. In case the user already wrapped commands with `'` by himself, this will not have an effect as we only add single quotes for escaping if they are not present already
23+
* [#211](https://github.com/Icinga/icinga-powershell-framework/pull/211) Adds feature to uninstall single components for Icinga for Windows or to uninstall everything and start entirely from new
2324

2425
### Bugfixes
2526

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<#
2+
.SYNOPSIS
3+
Uninstalls every PowerShell module within the icinga-powershell-* namespace
4+
including the Icinga Agent with all components (like certificates) as well as
5+
the Icinga for Windows service and the Icinga PowerShell Framework.
6+
.DESCRIPTION
7+
Uninstalls every PowerShell module within the icinga-powershell-* namespace
8+
including the Icinga Agent with all components (like certificates) as well as
9+
the Icinga for Windows service and the Icinga PowerShell Framework.
10+
.FUNCTIONALITY
11+
Uninstalls every PowerShell module within the icinga-powershell-* namespace
12+
including the Icinga Agent with all components (like certificates) as well as
13+
the Icinga for Windows service and the Icinga PowerShell Framework.
14+
.PARAMETER Force
15+
Suppress the question if you are sure to uninstall everything
16+
.INPUTS
17+
System.String
18+
.OUTPUTS
19+
Null
20+
.LINK
21+
https://github.com/Icinga/icinga-powershell-framework
22+
#>
23+
24+
function Uninstall-IcingaForWindows()
25+
{
26+
param (
27+
[switch]$Force = $FALSE
28+
);
29+
30+
$ModuleList = Get-Module 'icinga-powershell-*' -ListAvailable;
31+
[string]$Modules = [string]::Join(', ', $ModuleList.Name);
32+
33+
if ($Force -eq $FALSE) {
34+
Write-IcingaConsoleWarning -Message 'You are about to uninstall the Icinga Agent with all components (including certificates) and all Icinga for Windows Components: {0}{1}Are you sure you want to proceed? (y/N)' -Objects $Modules, (New-IcingaNewLine);
35+
$Input = Read-Host 'Confirm uninstall';
36+
if ($input -ne 'y') {
37+
return;
38+
}
39+
}
40+
41+
$CurrentLocation = Get-Location;
42+
43+
if ($CurrentLocation -eq (Join-Path -Path (Get-IcingaFrameworkRootPath) -ChildPath 'icinga-powershell-framework')) {
44+
Set-Location -Path (Get-IcingaFrameworkRootPath);
45+
}
46+
47+
Write-IcingaConsoleNotice 'Uninstalling Icinga for Windows from this host';
48+
Write-IcingaConsoleNotice 'Uninstalling Icinga Agent';
49+
Uninstall-IcingaAgent -RemoveDataFolder | Out-Null;
50+
Write-IcingaConsoleNotice 'Uninstalling Icinga for Windows service';
51+
Uninstall-IcingaFrameworkService | Out-Null;
52+
53+
$HasErrors = $FALSE;
54+
55+
foreach ($module in $ModuleList.Name) {
56+
[string]$ModuleName = $module.Replace('icinga-powershell-', '');
57+
58+
if ((Uninstall-IcingaFrameworkComponent -Component $ModuleName)) {
59+
continue;
60+
}
61+
62+
$HasErrors = $TRUE;
63+
}
64+
65+
Remove-Module 'icinga-powershell-framework' -Force -ErrorAction SilentlyContinue;
66+
67+
if ($HasErrors) {
68+
Write-IcingaConsoleWarning 'Not all components could be removed. Please ensure no other PowerShell/Application is currently open and accessing Icinga for Windows files';
69+
} else {
70+
Write-IcingaConsoleNotice 'Icinga for Windows was removed from this host.';
71+
}
72+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<#
2+
.SYNOPSIS
3+
Uninstalls a specific module within the icinga-powershell-* namespace
4+
inside your PowerShell module folder
5+
.DESCRIPTION
6+
Uninstalls a specific module within the icinga-powershell-* namespace
7+
inside your PowerShell module folder
8+
.FUNCTIONALITY
9+
Uninstalls a specific module within the icinga-powershell-* namespace
10+
inside your PowerShell module folder
11+
.PARAMETER Component
12+
The component you want to uninstall, like 'plugins' or 'mssql'
13+
.INPUTS
14+
System.String
15+
.OUTPUTS
16+
Null
17+
.LINK
18+
https://github.com/Icinga/icinga-powershell-framework
19+
#>
20+
21+
function Uninstall-IcingaFrameworkComponent()
22+
{
23+
param (
24+
[string]$Component
25+
);
26+
27+
$ModuleBase = Get-IcingaFrameworkRootPath;
28+
$UninstallComponent = [string]::Format('icinga-powershell-{0}', $Component);
29+
$UninstallPath = Join-Path -Path $ModuleBase -ChildPath $UninstallComponent;
30+
31+
if ((Test-Path $UninstallPath) -eq $FALSE) {
32+
Write-IcingaConsoleNotice -Message 'The Icinga for Windows component "{0}" at "{1}" could not ne found.' -Objects $UninstallComponent, $UninstallPath;
33+
return $FALSE;
34+
}
35+
36+
Write-IcingaConsoleNotice -Message 'Uninstalling Icinga for Windows component "{0}" from "{1}"' -Objects $UninstallComponent, $UninstallPath;
37+
if (Remove-ItemSecure -Path $UninstallPath -Recurse -Force) {
38+
Write-IcingaConsoleNotice -Message 'Successfully removed Icinga for Windows component "{0}" from "{1}"' -Objects $UninstallComponent, $UninstallPath;
39+
if ($UninstallComponent -ne 'icinga-powershell-framework') {
40+
Remove-Module $UninstallComponent -Force -ErrorAction SilentlyContinue;
41+
}
42+
return $TRUE;
43+
} else {
44+
Write-IcingaConsoleError -Message 'Unable to uninstall Icinga for Windows component "{0}" from "{1}"' -Objects $UninstallComponent, $UninstallPath;
45+
}
46+
47+
return $FALSE;
48+
}

0 commit comments

Comments
 (0)