Skip to content

Commit b97aca7

Browse files
committed
Adds command to repair Icinga Agent service
1 parent 269927c commit b97aca7

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
3838
* [#427](https://github.com/Icinga/icinga-powershell-framework/issues/427) Moves Icinga for Windows EventLog from `Application` to a custom log `Icinga for Windows`, allowing better separation
3939
* [#438](https://github.com/Icinga/icinga-powershell-framework/pull/438) Adds support for enabling the REST-Api background daemon and the Api-Check feature during the IMC installation wizard on advanced settings, which will be enabled by default
4040
* [#440](https://github.com/Icinga/icinga-powershell-framework/pull/440) Adds upgrade notification if Icinga for Windows Service binary older than v1.2.0 is used, which will not work with Icinga for Windows v1.8.0 or later
41+
* [#445](https://github.com/Icinga/icinga-powershell-framework/pull/445) Adds command `Repair-IcingaService` to repair Icinga Agent service in case it was broken during upgrades, mostly caused by `The specified service has been marked for deletion`
4142

4243
## 1.7.1 (2021-11-11)
4344

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<#
2+
.SYNOPSIS
3+
Repairs the Icinga Agent service installation in case the service is no longer present on the system
4+
caused by update failures
5+
.DESCRIPTION
6+
Repairs the Icinga Agent service installation in case the service is no longer present on the system
7+
caused by update failures
8+
.PARAMETER RootFolder
9+
Specifies the root folder of the Icinga Agent installation, in case a custom location is used for installation
10+
Default locations are detected automatically
11+
.EXAMPLE
12+
PS> Repair-IcingaService;
13+
.EXAMPLE
14+
PS> Repair-IcingaService -RootFolder 'D:\Programs\icinga2';
15+
.NOTES
16+
https://icinga.com/docs/icinga-for-windows/latest/doc/knowledgebase/IWKB000011/
17+
#>
18+
function Repair-IcingaService()
19+
{
20+
param (
21+
[string]$RootFolder = ''
22+
);
23+
24+
if ($null -ne (Get-Service 'icinga2' -ErrorAction SilentlyContinue)) {
25+
Write-IcingaConsoleNotice -Message 'The Icinga Agent service is already installed. If you received the error "The specified service has been marked for deletion", please have a look at https://icinga.com/docs/icinga-for-windows/latest/doc/knowledgebase/IWKB000011/'
26+
return;
27+
}
28+
29+
[string]$IcingaBinaryPath = 'sbin\icinga2.exe';
30+
[string]$IcingaServicePath = '';
31+
32+
if ([string]::IsNullOrEmpty($RootFolder) -eq $FALSE) {
33+
$IcingaServicePath = Join-Path -Path $RootFolder -ChildPath $IcingaBinaryPath;
34+
35+
if ((Test-Path $IcingaServicePath) -eq $FALSE) {
36+
Write-IcingaConsoleError `
37+
-Message 'The Icinga Agent could not be found at the location "{0}". Please specify only the Icinga Agent root path with "-RootFolder"' `
38+
-Objects $IcingaServicePath;
39+
40+
return;
41+
}
42+
} else {
43+
$IcingaServicePath = Join-Path -Path $Env:ProgramFiles -ChildPath ([string]::Format('ICINGA2\{0}', $IcingaBinaryPath));
44+
45+
if ((Test-Path $IcingaServicePath) -eq $FALSE) {
46+
$IcingaServicePath = Join-Path -Path ${Env:ProgramFiles(x86)} -ChildPath ([string]::Format('ICINGA2\{0}', $IcingaBinaryPath));
47+
48+
if ((Test-Path $IcingaServicePath) -eq $FALSE) {
49+
Write-IcingaConsoleError `
50+
-Message 'The Icinga Agent could not be found at the default locations "{0}" or "{1}". If you installed the Icinga Agent into a customer directory, please specify the root folder with "-RootFolder"' `
51+
-Objects $Env:ProgramFiles, ${Env:ProgramFiles(x86)};
52+
53+
return;
54+
}
55+
}
56+
}
57+
58+
Write-IcingaConsoleNotice `
59+
-Message 'Repairing Icinga Agent service with location "{0}"' `
60+
-Objects $IcingaServicePath;
61+
62+
$IcingaServicePath = [string]::Format('\"{0}\" --scm \"daemon\"', $IcingaServicePath);
63+
$IcingaService = Start-IcingaProcess -Executable 'sc.exe' -Arguments ([string]::Format('create icinga2 binPath= "{0}" DisplayName= "Icinga 2" start= auto', $IcingaServicePath));
64+
65+
if ($IcingaService.ExitCode -ne 0) {
66+
Write-IcingaConsoleError `
67+
-Message 'Failed to install Icinga Agent service: {0}{1}' `
68+
-Objects $IcingaService.Message, $IcingaService.Error;
69+
70+
return;
71+
}
72+
73+
$IcingaData = Get-IcingaAgentInstallation;
74+
$ConfigUser = Get-IcingaPowerShellConfig -Path 'Framework.Icinga.ServiceUser';
75+
$ServiceUser = $IcingaData.User;
76+
77+
if ([string]::IsNullOrEmpty($ConfigUser) -eq $FALSE) {
78+
$ServiceUser = $ConfigUser;
79+
}
80+
81+
Set-IcingaServiceUser -User $ServiceUser -SetPermission;
82+
Update-IcingaServiceUser;
83+
84+
Write-IcingaConsoleNotice -Message 'Icinga Agent service was successfully repaired. You can start it now with "Start-Service icinga2"';
85+
}

0 commit comments

Comments
 (0)