|
| 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