Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/100-General/10-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
* [#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
* [#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
* [#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
* [#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`

## 1.7.1 (2021-11-11)

Expand Down
85 changes: 85 additions & 0 deletions lib/core/icingaagent/installer/Repair-IcingaService.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<#
.SYNOPSIS
Repairs the Icinga Agent service installation in case the service is no longer present on the system
caused by update failures
.DESCRIPTION
Repairs the Icinga Agent service installation in case the service is no longer present on the system
caused by update failures
.PARAMETER RootFolder
Specifies the root folder of the Icinga Agent installation, in case a custom location is used for installation
Default locations are detected automatically
.EXAMPLE
PS> Repair-IcingaService;
.EXAMPLE
PS> Repair-IcingaService -RootFolder 'D:\Programs\icinga2';
.NOTES
https://icinga.com/docs/icinga-for-windows/latest/doc/knowledgebase/IWKB000011/
#>
function Repair-IcingaService()
{
param (
[string]$RootFolder = ''
);

if ($null -ne (Get-Service 'icinga2' -ErrorAction SilentlyContinue)) {
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/'
return;
}

[string]$IcingaBinaryPath = 'sbin\icinga2.exe';
[string]$IcingaServicePath = '';

if ([string]::IsNullOrEmpty($RootFolder) -eq $FALSE) {
$IcingaServicePath = Join-Path -Path $RootFolder -ChildPath $IcingaBinaryPath;

if ((Test-Path $IcingaServicePath) -eq $FALSE) {
Write-IcingaConsoleError `
-Message 'The Icinga Agent could not be found at the location "{0}". Please specify only the Icinga Agent root path with "-RootFolder"' `
-Objects $IcingaServicePath;

return;
}
} else {
$IcingaServicePath = Join-Path -Path $Env:ProgramFiles -ChildPath ([string]::Format('ICINGA2\{0}', $IcingaBinaryPath));

if ((Test-Path $IcingaServicePath) -eq $FALSE) {
$IcingaServicePath = Join-Path -Path ${Env:ProgramFiles(x86)} -ChildPath ([string]::Format('ICINGA2\{0}', $IcingaBinaryPath));

if ((Test-Path $IcingaServicePath) -eq $FALSE) {
Write-IcingaConsoleError `
-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"' `
-Objects $Env:ProgramFiles, ${Env:ProgramFiles(x86)};

return;
}
}
}

Write-IcingaConsoleNotice `
-Message 'Repairing Icinga Agent service with location "{0}"' `
-Objects $IcingaServicePath;

$IcingaServicePath = [string]::Format('\"{0}\" --scm \"daemon\"', $IcingaServicePath);
$IcingaService = Start-IcingaProcess -Executable 'sc.exe' -Arguments ([string]::Format('create icinga2 binPath= "{0}" DisplayName= "Icinga 2" start= auto', $IcingaServicePath));

if ($IcingaService.ExitCode -ne 0) {
Write-IcingaConsoleError `
-Message 'Failed to install Icinga Agent service: {0}{1}' `
-Objects $IcingaService.Message, $IcingaService.Error;

return;
}

$IcingaData = Get-IcingaAgentInstallation;
$ConfigUser = Get-IcingaPowerShellConfig -Path 'Framework.Icinga.ServiceUser';
$ServiceUser = $IcingaData.User;

if ([string]::IsNullOrEmpty($ConfigUser) -eq $FALSE) {
$ServiceUser = $ConfigUser;
}

Set-IcingaServiceUser -User $ServiceUser -SetPermission;
Update-IcingaServiceUser;

Write-IcingaConsoleNotice -Message 'Icinga Agent service was successfully repaired. You can start it now with "Start-Service icinga2"';
}