From 0b1de9e9c00e8e461761b63243f6e59e36f6e5fc Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Fri, 14 Apr 2023 12:29:46 +0200 Subject: [PATCH] Adds feature to offload service and JEA actions to scheduled tasks --- ...lear-IcingaInternalServiceInformation.psm1 | 8 +++ .../Get-IcingaWindowsServiceStatus.psm1 | 53 +++++++++++++++++++ .../Install-IcingaForWindowsService.psm1 | 6 +-- .../Install-IcingaFrameworkComponent.psm1 | 2 +- .../Install-IcingaFrameworkUpdate.psm1 | 6 +-- .../Invoke-IcingaForWindowsMigration.psm1 | 4 +- .../Invoke-IcingaInternalServiceCall.psm1 | 2 +- .../New-IcingaEnvironmentVariable.psm1 | 5 ++ lib/core/framework/Restart-IcingaService.psm1 | 30 +++++------ lib/core/framework/Start-IcingaService.psm1 | 32 ++++++----- lib/core/framework/Stop-IcingaService.psm1 | 32 ++++++----- .../getters/Get-IcingaAgentInstallation.psm1 | 11 +--- .../getters/Get-IcingaServiceUser.psm1 | 26 ++++++--- .../installer/Install-IcingaAgent.psm1 | 13 +---- .../installer/Uninstall-IcingaAgent.psm1 | 12 +---- .../misc/Clear-IcingaAgentApiDirectory.psm1 | 8 +-- .../misc/Start-IcingaAgentInstallWizard.psm1 | 2 +- .../repair/Repair-IcingaService.psm1 | 2 +- .../setters/Set-IcingaAgentServiceUser.psm1 | 8 ++- .../icingaagent/tests/Test-IcingaAgent.psm1 | 10 ++-- lib/core/installer/Install-Icinga.psm1 | 9 +++- .../Start-IcingaForWindowsInstallation.psm1 | 16 +++++- .../installer/menu/manage/general/Manage.psm1 | 4 +- .../menu/manage/general/RemoveComponents.psm1 | 4 +- .../settings/services/ManageServices.psm1 | 34 ++++++------ .../troubleshooting/Troubleshooting.psm1 | 4 +- lib/core/jea/Install-IcingaJeaProfile.psm1 | 2 +- lib/core/jea/Register-IcingaJEAProfile.psm1 | 2 +- .../repository/Install-IcingaComponent.psm1 | 24 ++------- .../Disable-IcingaServiceRecovery.psm1 | 4 +- .../windows/Enable-IcingaServiceRecovery.psm1 | 4 +- lib/core/windows/Install-IcingaSecurity.psm1 | 10 +++- .../windows/Restart-IcingaWindowsService.psm1 | 12 ++++- .../windows/Uninstall-IcingaServiceUser.psm1 | 4 +- 34 files changed, 237 insertions(+), 168 deletions(-) create mode 100644 lib/core/framework/Clear-IcingaInternalServiceInformation.psm1 create mode 100644 lib/core/framework/Get-IcingaWindowsServiceStatus.psm1 diff --git a/lib/core/framework/Clear-IcingaInternalServiceInformation.psm1 b/lib/core/framework/Clear-IcingaInternalServiceInformation.psm1 new file mode 100644 index 00000000..ac43321e --- /dev/null +++ b/lib/core/framework/Clear-IcingaInternalServiceInformation.psm1 @@ -0,0 +1,8 @@ +function Clear-IcingaInternalServiceInformation() +{ + $Global:Icinga.Protected.ServiceRestartLock = $FALSE; + $Global:Icinga.Protected.IcingaServiceUser = ''; + $Global:Icinga.Protected.IfWServiceUser = ''; + $Global:Icinga.Protected.IcingaServiceState = ''; + $Global:Icinga.Protected.IfWServiceState = ''; +} diff --git a/lib/core/framework/Get-IcingaWindowsServiceStatus.psm1 b/lib/core/framework/Get-IcingaWindowsServiceStatus.psm1 new file mode 100644 index 00000000..09011a1d --- /dev/null +++ b/lib/core/framework/Get-IcingaWindowsServiceStatus.psm1 @@ -0,0 +1,53 @@ +function Get-IcingaWindowsServiceStatus() +{ + param ( + [string]$Service = '', + [switch]$Force = $FALSE + ); + + if ($Service -eq 'icinga2' -Or $Service -eq 'icingapowershell') { + if ($Service -eq 'icinga2') { + if ([string]::IsNullOrEmpty($Global:Icinga.Protected.IcingaServiceState) -eq $FALSE) { + if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) { + return @{ + 'Status' = $Global:Icinga.Protected.IcingaServiceState; + 'Present' = $TRUE; + 'Name' = $Service; + 'DisplayName' = $Service; + }; + } + } + } elseif ($Service -eq 'icingapowershell') { + if ([string]::IsNullOrEmpty($Global:Icinga.Protected.IfWServiceState) -eq $FALSE) { + if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) { + return @{ + 'Status' = $Global:Icinga.Protected.IfWServiceState; + 'Present' = $TRUE; + 'Name' = $Service; + 'DisplayName' = $Service; + }; + } + } + } + } + + $ServiceData = Invoke-IcingaWindowsScheduledTask -JobType 'GetWindowsService' -ObjectName $Service; + + if ($ServiceData.Service.Installed -eq $FALSE) { + Write-IcingaConsoleError $ServiceData.ErrMsg; + return @{ + 'Status' = ''; + 'Present' = $FALSE; + 'Name' = 'Unknown'; + 'DisplayName' = 'Unknown'; + }; + } + + if ($Service -eq 'icinga2') { + $Global:Icinga.Protected.IcingaServiceState = $ServiceData.Service.Status; + } elseif ($Service -eq 'icingapowershell') { + $Global:Icinga.Protected.IfWServiceState = $ServiceData.Service.Status; + } + + return $ServiceData.Service; +} diff --git a/lib/core/framework/Install-IcingaForWindowsService.psm1 b/lib/core/framework/Install-IcingaForWindowsService.psm1 index ef29f07d..8f267be4 100644 --- a/lib/core/framework/Install-IcingaForWindowsService.psm1 +++ b/lib/core/framework/Install-IcingaForWindowsService.psm1 @@ -39,13 +39,13 @@ function Install-IcingaForWindowsService() $UpdateFile = [string]::Format('{0}.update', $Path); - $ServiceStatus = (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue).Status; + $ServiceStatus = Get-IcingaWindowsServiceStatus -Service 'icingapowershell'; if ((Test-Path $UpdateFile)) { Write-IcingaConsoleNotice 'Updating Icinga PowerShell Service binary'; - if ($ServiceStatus -eq 'Running') { + if ($ServiceStatus.Status -eq 'Running') { Write-IcingaConsoleNotice 'Stopping Icinga PowerShell service'; Stop-IcingaWindowsService; Start-Sleep -Seconds 1; @@ -68,7 +68,7 @@ function Install-IcingaForWindowsService() (Get-IcingaPowerShellModuleFile) ); - if ($null -eq $ServiceStatus) { + if ($ServiceStatus.Present -eq $FALSE) { $ServiceCreation = Start-IcingaProcess -Executable 'sc.exe' -Arguments ([string]::Format('create icingapowershell binPath= "{0}" DisplayName= "Icinga PowerShell Service" start= auto', $Path)); if ($ServiceCreation.ExitCode -ne 0) { diff --git a/lib/core/framework/Install-IcingaFrameworkComponent.psm1 b/lib/core/framework/Install-IcingaFrameworkComponent.psm1 index da3dbf79..440f776e 100644 --- a/lib/core/framework/Install-IcingaFrameworkComponent.psm1 +++ b/lib/core/framework/Install-IcingaFrameworkComponent.psm1 @@ -112,7 +112,7 @@ function Install-IcingaFrameworkComponent() if ([string]::IsNullOrEmpty((Get-IcingaJEAContext)) -eq $FALSE) { Write-IcingaConsoleNotice 'Updating Icinga JEA profile'; - & powershell.exe -Command { Use-Icinga -Minimal; Install-IcingaJEAProfile; } | Out-Null; + Invoke-IcingaWindowsScheduledTask -JobType InstallJEA -Timeout 600 | Out-Null; } # Unload the module if it was loaded before diff --git a/lib/core/framework/Install-IcingaFrameworkUpdate.psm1 b/lib/core/framework/Install-IcingaFrameworkUpdate.psm1 index 6f0afb14..84a3dc9e 100644 --- a/lib/core/framework/Install-IcingaFrameworkUpdate.psm1 +++ b/lib/core/framework/Install-IcingaFrameworkUpdate.psm1 @@ -52,8 +52,8 @@ function Install-IcingaFrameworkUpdate() Write-IcingaConsoleNotice ([string]::Format('Using content of folder "{0}" for updates', $ModuleContent)); - $ServiceStatus = (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue).Status; - $AgentStatus = (Get-Service 'icinga2' -ErrorAction SilentlyContinue).Status; + $ServiceStatus = (Get-IcingaWindowsServiceStatus -Service 'icingapowershell').Status; + $AgentStatus = (Get-IcingaWindowsServiceStatus -Service 'icinga2').Status; if ($ServiceStatus -eq 'Running') { Write-IcingaConsoleNotice 'Stopping Icinga PowerShell service'; @@ -109,7 +109,7 @@ function Install-IcingaFrameworkUpdate() if ([string]::IsNullOrEmpty((Get-IcingaJEAContext)) -eq $FALSE) { Remove-IcingaFrameworkDependencyFile; Write-IcingaConsoleNotice 'Updating Icinga JEA profile'; - & powershell.exe -Command { Use-Icinga -Minimal; Install-IcingaJEAProfile; } | Out-Null; + Invoke-IcingaWindowsScheduledTask -JobType InstallJEA -Timeout 600 | Out-Null; } Write-IcingaConsoleNotice 'Framework update has been completed. Please start a new PowerShell instance now to complete the update'; diff --git a/lib/core/framework/Invoke-IcingaForWindowsMigration.psm1 b/lib/core/framework/Invoke-IcingaForWindowsMigration.psm1 index c7a8a4eb..c4cfefe9 100644 --- a/lib/core/framework/Invoke-IcingaForWindowsMigration.psm1 +++ b/lib/core/framework/Invoke-IcingaForWindowsMigration.psm1 @@ -13,7 +13,7 @@ function Invoke-IcingaForWindowsMigration() # Upgrade to v1.8.0 if (Test-IcingaForWindowsMigration -MigrationVersion (New-IcingaVersionObject -Version '1.8.0')) { - $ServiceStatus = (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue).Status; + $ServiceStatus = (Get-IcingaWindowsServiceStatus -Service 'icingapowershell').Status; Write-IcingaConsoleNotice 'Applying pending migrations required for Icinga for Windows v1.8.0'; if ($ServiceStatus -eq 'Running') { @@ -43,7 +43,7 @@ function Invoke-IcingaForWindowsMigration() if (Test-IcingaForWindowsMigration -MigrationVersion (New-IcingaVersionObject -Version '1.10.0')) { Write-IcingaConsoleNotice 'Applying pending migrations required for Icinga for Windows v1.10.0'; - $ServiceStatus = (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue).Status; + $ServiceStatus = (Get-IcingaWindowsServiceStatus -Service 'icingapowershell').Status; if ($ServiceStatus -eq 'Running') { Stop-IcingaWindowsService; diff --git a/lib/core/framework/Invoke-IcingaInternalServiceCall.psm1 b/lib/core/framework/Invoke-IcingaInternalServiceCall.psm1 index 3d99ebcf..5ce530f2 100644 --- a/lib/core/framework/Invoke-IcingaInternalServiceCall.psm1 +++ b/lib/core/framework/Invoke-IcingaInternalServiceCall.psm1 @@ -17,7 +17,7 @@ function Invoke-IcingaInternalServiceCall() } # Test our Icinga for Windows service. If the service is not installed or not running, execute the plugin locally - $IcingaForWindowsService = (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue); + $IcingaForWindowsService = Get-Service 'icingapowershell' -ErrorAction SilentlyContinue; if ($null -eq $IcingaForWindowsService -Or $IcingaForWindowsService.Status -ne 'Running') { return $NULL; diff --git a/lib/core/framework/New-IcingaEnvironmentVariable.psm1 b/lib/core/framework/New-IcingaEnvironmentVariable.psm1 index 0d7a1dad..9145c0cc 100644 --- a/lib/core/framework/New-IcingaEnvironmentVariable.psm1 +++ b/lib/core/framework/New-IcingaEnvironmentVariable.psm1 @@ -68,6 +68,11 @@ function New-IcingaEnvironmentVariable() $Global:Icinga.Protected.Add('RunAsDaemon', $FALSE); $Global:Icinga.Protected.Add('Minimal', $FALSE); $Global:Icinga.Protected.Add('ThreadName', ''); + $Global:Icinga.Protected.Add('IcingaServiceUser', ''); + $Global:Icinga.Protected.Add('IfWServiceUser', ''); + $Global:Icinga.Protected.Add('ServiceRestartLock', $FALSE); + $Global:Icinga.Protected.Add('IcingaServiceState', ''); + $Global:Icinga.Protected.Add('IfWServiceState', ''); $Global:Icinga.Protected.Add('GarbageCollector', @{ }); } } diff --git a/lib/core/framework/Restart-IcingaService.psm1 b/lib/core/framework/Restart-IcingaService.psm1 index ff89a739..4e4f9ae2 100644 --- a/lib/core/framework/Restart-IcingaService.psm1 +++ b/lib/core/framework/Restart-IcingaService.psm1 @@ -21,27 +21,25 @@ function Restart-IcingaService() { param ( - $Service + $Service, + [switch]$Force = $FALSE ); - if (Get-Service "$Service" -ErrorAction SilentlyContinue) { - Write-IcingaConsoleNotice ([string]::Format('Restarting service "{0}"', $Service)); + if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) { + return; + } - & powershell.exe -Command { - Use-Icinga -Minimal; + $Result = Invoke-IcingaWindowsScheduledTask -JobType 'RestartWindowsService' -ObjectName $Service; - $Service = $args[0]; - try { - Restart-Service "$Service" -ErrorAction Stop; - Start-Sleep -Seconds 2; - Optimize-IcingaForWindowsMemory; - } catch { - Write-IcingaConsoleError -Message 'Failed to restart service "{0}". Error: {1}' -Objects $Service, $_.Exception.Message; - } - } -Args $Service; + if ($Result.Success -eq $FALSE) { + Write-IcingaConsoleError $Result.ErrMsg; } else { - Write-IcingaConsoleWarning -Message 'The service "{0}" is not installed' -Objects $Service; + Write-IcingaConsoleNotice $Result.Message; } - Optimize-IcingaForWindowsMemory; + if ($Service -eq 'icinga2') { + $Global:Icinga.Protected.IcingaServiceState = $Result.Status; + } elseif ($Service -eq 'icingapowershell') { + $Global:Icinga.Protected.IfWServiceState = $Result.Status; + } } diff --git a/lib/core/framework/Start-IcingaService.psm1 b/lib/core/framework/Start-IcingaService.psm1 index fd240876..88589065 100644 --- a/lib/core/framework/Start-IcingaService.psm1 +++ b/lib/core/framework/Start-IcingaService.psm1 @@ -20,28 +20,26 @@ function Start-IcingaService() { - param( - $Service + param ( + $Service, + [switch]$Force = $FALSE ); - if (Get-Service $Service -ErrorAction SilentlyContinue) { - Write-IcingaConsoleNotice -Message 'Starting service "{0}"' -Objects $Service; + if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) { + return; + } - & powershell.exe -Command { - Use-Icinga -Minimal; + $Result = Invoke-IcingaWindowsScheduledTask -JobType 'StartWindowsService' -ObjectName $Service; - $Service = $args[0]; - try { - Start-Service "$Service" -ErrorAction Stop; - Start-Sleep -Seconds 2; - Optimize-IcingaForWindowsMemory; - } catch { - Write-IcingaConsoleError -Message 'Failed to start service "{0}". Error: {1}' -Objects $Service, $_.Exception.Message; - } - } -Args $Service; + if ($Result.Success -eq $FALSE) { + Write-IcingaConsoleError $Result.ErrMsg; } else { - Write-IcingaConsoleWarning -Message 'The service "{0}" is not installed' -Objects $Service; + Write-IcingaConsoleNotice $Result.Message; } - Optimize-IcingaForWindowsMemory; + if ($Service -eq 'icinga2') { + $Global:Icinga.Protected.IcingaServiceState = $Result.Status; + } elseif ($Service -eq 'icingapowershell') { + $Global:Icinga.Protected.IfWServiceState = $Result.Status; + } } diff --git a/lib/core/framework/Stop-IcingaService.psm1 b/lib/core/framework/Stop-IcingaService.psm1 index 4d569629..57456732 100644 --- a/lib/core/framework/Stop-IcingaService.psm1 +++ b/lib/core/framework/Stop-IcingaService.psm1 @@ -20,28 +20,26 @@ function Stop-IcingaService() { - param( - $Service + param ( + $Service, + [switch]$Force = $FALSE ); - if (Get-Service "$Service" -ErrorAction SilentlyContinue) { - Write-IcingaConsoleNotice -Message 'Stopping service "{0}"' -Objects $Service; + if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) { + return; + } - & powershell.exe -Command { - Use-Icinga -Minimal; + $Result = Invoke-IcingaWindowsScheduledTask -JobType 'StopWindowsService' -ObjectName $Service; - $Service = $args[0]; - try { - Stop-Service "$Service" -ErrorAction Stop; - Start-Sleep -Seconds 2; - Optimize-IcingaForWindowsMemory; - } catch { - Write-IcingaConsoleError -Message 'Failed to stop service "{0}". Error: {1}' -Objects $Service, $_.Exception.Message; - } - } -Args $Service; + if ($Result.Success -eq $FALSE) { + Write-IcingaConsoleError $Result.ErrMsg; } else { - Write-IcingaConsoleWarning -Message 'The service "{0}" is not installed' -Objects $Service; + Write-IcingaConsoleNotice $Result.Message; } - Optimize-IcingaForWindowsMemory; + if ($Service -eq 'icinga2') { + $Global:Icinga.Protected.IcingaServiceState = $Result.Status; + } elseif ($Service -eq 'icingapowershell') { + $Global:Icinga.Protected.IfWServiceState = $Result.Status; + } } diff --git a/lib/core/icingaagent/getters/Get-IcingaAgentInstallation.psm1 b/lib/core/icingaagent/getters/Get-IcingaAgentInstallation.psm1 index 00be24b0..126331a5 100644 --- a/lib/core/icingaagent/getters/Get-IcingaAgentInstallation.psm1 +++ b/lib/core/icingaagent/getters/Get-IcingaAgentInstallation.psm1 @@ -18,13 +18,6 @@ function Get-IcingaAgentInstallation() } } - $IcingaService = Get-IcingaServices -Service 'icinga2'; - $ServiceUser = 'NT AUTHORITY\NetworkService'; - - if ($null -ne $IcingaService) { - $ServiceUser = $IcingaService.icinga2.configuration.ServiceUser; - } - if ($null -eq $IcingaData) { return @{ 'Installed' = $FALSE; @@ -33,7 +26,7 @@ function Get-IcingaAgentInstallation() 'Architecture' = $architecture; 'Uninstaller' = ''; 'InstallDate' = ''; - 'User' = $ServiceUser; + 'User' = (Get-IcingaServiceUser); }; } @@ -44,6 +37,6 @@ function Get-IcingaAgentInstallation() 'Architecture' = $architecture; 'Uninstaller' = $IcingaData.UninstallString.Replace("MsiExec.exe ", ""); 'InstallDate' = $IcingaData.InstallDate; - 'User' = $ServiceUser; + 'User' = (Get-IcingaServiceUser); }; } diff --git a/lib/core/icingaagent/getters/Get-IcingaServiceUser.psm1 b/lib/core/icingaagent/getters/Get-IcingaServiceUser.psm1 index 1a1d1446..0bf4054c 100644 --- a/lib/core/icingaagent/getters/Get-IcingaServiceUser.psm1 +++ b/lib/core/icingaagent/getters/Get-IcingaServiceUser.psm1 @@ -1,19 +1,31 @@ function Get-IcingaServiceUser() { - $Services = Get-IcingaServices -Service 'icinga2'; - if ($null -eq $Services) { - $Services = Get-IcingaServices -Service 'icingapowershell'; - if ($null -eq $Services) { - return $null; + if ([string]::IsNullOrEmpty($Global:Icinga.Protected.IcingaServiceUser) -eq $FALSE) { + return $Global:Icinga.Protected.IcingaServiceUser; + } + + $Services = Get-IcingaWindowsServiceStatus -Service 'icinga2'; + if ($Services.Present -eq $FALSE) { + $Services = Get-IcingaWindowsServiceStatus -Service 'icingapowershell'; + if ($Services.Present -eq $FALSE) { + return 'NT Authority\NetworkService'; } } - $Services = $Services.GetEnumerator() | Select-Object -First 1; - $ServiceUser = ($Services.Value.configuration.ServiceUser).Replace('.\', ''); + $ServiceUser = (Get-IcingaWindowsInformation Win32_Service | + ForEach-Object { + if ($_.Name -Like $Services.Name) { + return $_; + } + } | Select-Object StartName).StartName; + + $ServiceUser = $ServiceUser.Replace('.\', ''); if ($ServiceUser -eq 'LocalSystem') { $ServiceUser = 'NT Authority\SYSTEM'; } + $Global:Icinga.Protected.IcingaServiceUser = $ServiceUser; + return $ServiceUser; } diff --git a/lib/core/icingaagent/installer/Install-IcingaAgent.psm1 b/lib/core/icingaagent/installer/Install-IcingaAgent.psm1 index 5df2111f..f59de340 100644 --- a/lib/core/icingaagent/installer/Install-IcingaAgent.psm1 +++ b/lib/core/icingaagent/installer/Install-IcingaAgent.psm1 @@ -73,18 +73,7 @@ function Install-IcingaAgent() } } - $InstallProcess = & powershell.exe -Command { - Use-Icinga -Minimal; - - $IcingaInstaller = $args[0]; - $InstallTarget = $args[1]; - $InstallProcess = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('/quiet /norestart /i "{0}" {1}', $IcingaInstaller.InstallerPath, $InstallTarget)) -FlushNewLines; - - Start-Sleep -Seconds 2; - Optimize-IcingaForWindowsMemory; - - return $InstallProcess; - } -Args $IcingaInstaller, $InstallTarget; + $InstallProcess = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('/quiet /i "{0}" {1}', $IcingaInstaller.InstallerPath, $InstallTarget)) -FlushNewLines; if ($InstallProcess.ExitCode -ne 0) { Write-IcingaConsoleError -Message 'Failed to install Icinga 2 Agent: {0}{1}' -Objects $InstallProcess.Message, $InstallProcess.Error; diff --git a/lib/core/icingaagent/installer/Uninstall-IcingaAgent.psm1 b/lib/core/icingaagent/installer/Uninstall-IcingaAgent.psm1 index 5a55b06c..b8cf842a 100644 --- a/lib/core/icingaagent/installer/Uninstall-IcingaAgent.psm1 +++ b/lib/core/icingaagent/installer/Uninstall-IcingaAgent.psm1 @@ -22,17 +22,7 @@ function Uninstall-IcingaAgent() 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 /norestart', $IcingaData.Uninstaller)) -FlushNewLine; - - Start-Sleep -Seconds 2; - Optimize-IcingaForWindowsMemory; - - return $Uninstaller; - } -Args $IcingaData; + $Uninstaller = Invoke-IcingaWindowsScheduledTask -JobType UninstallAgent -FilePath $IcingaData.Uninstaller; if ($Uninstaller.ExitCode -ne 0) { Write-IcingaConsoleError ([string]::Format('Failed to remove Icinga Agent: {0}{1}', $Uninstaller.Message, $Uninstaller.Error)); diff --git a/lib/core/icingaagent/misc/Clear-IcingaAgentApiDirectory.psm1 b/lib/core/icingaagent/misc/Clear-IcingaAgentApiDirectory.psm1 index 55be2495..90f9117c 100644 --- a/lib/core/icingaagent/misc/Clear-IcingaAgentApiDirectory.psm1 +++ b/lib/core/icingaagent/misc/Clear-IcingaAgentApiDirectory.psm1 @@ -28,7 +28,7 @@ function Clear-IcingaAgentApiDirectory() [switch]$Force = $FALSE ); - $IcingaService = (Get-IcingaServices -Service icinga2).icinga2; + $IcingaService = Get-IcingaWindowsServiceStatus -Service 'icinga2'; $ApiDirectory = (Join-Path -Path $Env:ProgramData -ChildPath 'icinga2\var\lib\icinga2\api\'); if ((Test-Path $ApiDirectory) -eq $FALSE) { @@ -36,12 +36,12 @@ function Clear-IcingaAgentApiDirectory() return; } - if ($IcingaService.configuration.Status.raw -eq 4 -And $Force -eq $FALSE) { + if ($IcingaService.Status -eq 'Running' -And $Force -eq $FALSE) { Write-IcingaConsoleError 'The API directory can not be deleted while the Icinga Agent is running. Use the "-Force" argument to stop the service, flush the directory and restart the service again.'; return; } - if ($IcingaService.configuration.Status.raw -eq 4) { + if ($IcingaService.Status -eq 'Running') { Stop-IcingaService icinga2; Start-Sleep -Seconds 1; } @@ -50,7 +50,7 @@ function Clear-IcingaAgentApiDirectory() Remove-ItemSecure -Path (Join-Path -Path $ApiDirectory -ChildPath '*') -Recurse -Force | Out-Null; Start-Sleep -Seconds 1; - if ($IcingaService.configuration.Status.raw -eq 4) { + if ($IcingaService.Status -eq 'Running') { Start-IcingaService icinga2; } } diff --git a/lib/core/icingaagent/misc/Start-IcingaAgentInstallWizard.psm1 b/lib/core/icingaagent/misc/Start-IcingaAgentInstallWizard.psm1 index 9a12d1ba..70ad45c7 100644 --- a/lib/core/icingaagent/misc/Start-IcingaAgentInstallWizard.psm1 +++ b/lib/core/icingaagent/misc/Start-IcingaAgentInstallWizard.psm1 @@ -754,7 +754,7 @@ function Start-IcingaAgentInstallWizard() if ($InstallFrameworkService) { Restart-IcingaForWindows; } - Restart-IcingaService 'icinga2'; + Restart-IcingaService 'icinga2' -Force; } } } diff --git a/lib/core/icingaagent/repair/Repair-IcingaService.psm1 b/lib/core/icingaagent/repair/Repair-IcingaService.psm1 index 885e41b6..45bd44d6 100644 --- a/lib/core/icingaagent/repair/Repair-IcingaService.psm1 +++ b/lib/core/icingaagent/repair/Repair-IcingaService.psm1 @@ -21,7 +21,7 @@ function Repair-IcingaService() [string]$RootFolder = '' ); - if ($null -ne (Get-Service 'icinga2' -ErrorAction SilentlyContinue)) { + if ((Get-IcingaWindowsServiceStatus -Service 'icinga2').Present) { 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; } diff --git a/lib/core/icingaagent/setters/Set-IcingaAgentServiceUser.psm1 b/lib/core/icingaagent/setters/Set-IcingaAgentServiceUser.psm1 index 8a8696a2..82d321cb 100644 --- a/lib/core/icingaagent/setters/Set-IcingaAgentServiceUser.psm1 +++ b/lib/core/icingaagent/setters/Set-IcingaAgentServiceUser.psm1 @@ -12,7 +12,7 @@ function Set-IcingaServiceUser() return $FALSE; } - if ($null -eq (Get-Service $Service -ErrorAction SilentlyContinue)) { + if ((Get-IcingaWindowsServiceStatus -Service $Service).Present -eq $FALSE) { return $FALSE; } @@ -40,6 +40,12 @@ function Set-IcingaServiceUser() Set-IcingaUserPermissions; } + if ($Service -eq 'icinga2') { + $Global:Icinga.Protected.IcingaServiceUser = $User; + } elseif ($Service -eq 'icingapowershell') { + $Global:Icinga.Protected.IfWServiceUser = $User; + } + Write-IcingaConsoleNotice 'Service User "{0}" for service "{1}" successfully updated' -Objects $User, $Service; return $TRUE; } else { diff --git a/lib/core/icingaagent/tests/Test-IcingaAgent.psm1 b/lib/core/icingaagent/tests/Test-IcingaAgent.psm1 index fd127b74..e05693e6 100644 --- a/lib/core/icingaagent/tests/Test-IcingaAgent.psm1 +++ b/lib/core/icingaagent/tests/Test-IcingaAgent.psm1 @@ -1,14 +1,14 @@ function Test-IcingaAgent() { $IcingaAgentData = Get-IcingaAgentInstallation; - $AgentServicePresent = Get-Service 'icinga2' -ErrorAction SilentlyContinue; - if ($IcingaAgentData.Installed -And $null -ne $AgentServicePresent) { + $AgentServicePresent = Get-IcingaWindowsServiceStatus -Service 'icinga2'; + if ($IcingaAgentData.Installed -And $AgentServicePresent.Present) { Write-IcingaTestOutput -Severity 'Passed' -Message 'Icinga Agent service is installed'; - } elseif ($IcingaAgentData.Installed -And $null -eq $AgentServicePresent) { + } elseif ($IcingaAgentData.Installed -And $AgentServicePresent.Present -eq $FALSE) { Write-IcingaTestOutput -Severity 'Failed' -Message 'Icinga Agent service is not installed'; - } elseif ($IcingaAgentData.Installed -eq $FALSE -And $null -ne $AgentServicePresent) { + } elseif ($IcingaAgentData.Installed -eq $FALSE -And $AgentServicePresent.Present) { Write-IcingaTestOutput -Severity 'Failed' -Message 'Icinga Agent service is still present, while Icinga Agent itself is not installed.'; - } elseif ($IcingaAgentData.Installed -eq $FALSE -And $null -eq $AgentServicePresent) { + } elseif ($IcingaAgentData.Installed -eq $FALSE -And $AgentServicePresent.Present -eq $FALSE) { Write-IcingaTestOutput -Severity 'Passed' -Message 'Icinga Agent is not installed and service is not present.'; return; diff --git a/lib/core/installer/Install-Icinga.psm1 b/lib/core/installer/Install-Icinga.psm1 index 4d6090c6..73b969c2 100644 --- a/lib/core/installer/Install-Icinga.psm1 +++ b/lib/core/installer/Install-Icinga.psm1 @@ -13,6 +13,8 @@ function Install-Icinga() # Always ensure we use the proper TLS Version Set-IcingaTLSVersion; + $Global:Icinga.Protected.ServiceRestartLock = $TRUE; + # Ignore SSL validation in case we set the flag if ($NoSSLValidation) { Enable-IcingaUntrustedCertificateValidation; @@ -68,6 +70,7 @@ function Install-Icinga() $JsonInstallCmd = ConvertFrom-Json -InputObject $InstallCommand -ErrorAction Stop; } catch { Write-IcingaConsoleError 'Failed to deserialize the provided JSON from file or command: {0}' -Objects $_.Exception.Message; + Clear-IcingaInternalServiceInformation; return; } @@ -81,6 +84,7 @@ function Install-Icinga() $Success = Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration; if ($Success -eq $FALSE) { + Clear-IcingaInternalServiceInformation; return; } @@ -101,6 +105,7 @@ function Install-Icinga() $Success = Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration; if ($Success -eq $FALSE) { + Clear-IcingaInternalServiceInformation; return; } @@ -111,7 +116,7 @@ function Install-Icinga() # Set our "old" swap live again. By doing so, we can still continue our old # configuration Set-IcingaPowerShellConfig -Path 'Framework.Config.Swap' -Value $OldConfigSwap; - + Clear-IcingaInternalServiceInformation; return; } @@ -186,6 +191,8 @@ function Install-Icinga() } } + Clear-IcingaInternalServiceInformation; + if ($null -ne (Get-Command -Name 'Set-IcingaForWindowsManagementConsoleClosing' -ErrorAction SilentlyContinue)) { Set-IcingaForWindowsManagementConsoleClosing -Completed; } diff --git a/lib/core/installer/Start-IcingaForWindowsInstallation.psm1 b/lib/core/installer/Start-IcingaForWindowsInstallation.psm1 index 9ec7970c..4aa02e93 100644 --- a/lib/core/installer/Start-IcingaForWindowsInstallation.psm1 +++ b/lib/core/installer/Start-IcingaForWindowsInstallation.psm1 @@ -74,7 +74,6 @@ function Start-IcingaForWindowsInstallation() $PluginPackageRelease = $FALSE; $PluginPackageSnapshot = $FALSE; $ForceCertificateGen = $FALSE; - [bool]$InstallJEA = $FALSE; [bool]$InstallRESTApi = $FALSE; if ([string]::IsNullOrEmpty($IcingaStableRepo) -eq $FALSE) { @@ -323,6 +322,21 @@ function Start-IcingaForWindowsInstallation() $global:Icinga.InstallWizard.Config = @{ }; Set-IcingaPowerShellConfig -Path 'Framework.Installed' -Value $TRUE; + # Always install the JEA profile at the end + switch ($InstallJEAProfile) { + '0' { + Invoke-IcingaWindowsScheduledTask -JobType InstallJEA -Timeout 600 | Out-Null; + break; + }; + '1' { + Install-IcingaSecurity -RunAsTask; + break; + }; + '2' { + # Do not install JEA profile + }; + } + if ($Automated -eq $FALSE) { Write-IcingaConsoleNotice 'Icinga for Windows is installed. Returning to main menu in 5 seconds' Start-Sleep -Seconds 5; diff --git a/lib/core/installer/menu/manage/general/Manage.psm1 b/lib/core/installer/menu/manage/general/Manage.psm1 index 99ca9f13..6f742523 100644 --- a/lib/core/installer/menu/manage/general/Manage.psm1 +++ b/lib/core/installer/menu/manage/general/Manage.psm1 @@ -14,7 +14,7 @@ function Show-IcingaForWindowsMenuManage() 'Caption' = 'Services'; 'Command' = 'Show-IcingaForWindowsMenuManageIcingaForWindowsServices'; 'Help' = 'Allows you to manage the Icinga Agent and Icinga for Windows service'; - 'Disabled' = (-Not $AgentInstalled -And -Not ([bool](Get-Service 'icingapowershell' -ErrorAction SilentlyContinue))); + 'Disabled' = (-Not $AgentInstalled -And -Not (Get-IcingaWindowsServiceStatus -Service 'icingapowershell').Present); 'AdminMenu' = $TRUE; }, @{ @@ -28,7 +28,7 @@ function Show-IcingaForWindowsMenuManage() 'Caption' = 'Background Daemons'; 'Command' = 'Show-IcingaForWindowsManagementConsoleManageBackgroundDaemons'; 'Help' = 'Allows you to manage Icinga for Windows background daemons'; - 'Disabled' = ($null -eq (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue)); + 'Disabled' = (-Not (Get-IcingaWindowsServiceStatus -Service 'icingapowershell').Present); 'DisabledReason' = 'Icinga for Windows service is not installed'; }, @{ diff --git a/lib/core/installer/menu/manage/general/RemoveComponents.psm1 b/lib/core/installer/menu/manage/general/RemoveComponents.psm1 index c03747f1..4c83469e 100644 --- a/lib/core/installer/menu/manage/general/RemoveComponents.psm1 +++ b/lib/core/installer/menu/manage/general/RemoveComponents.psm1 @@ -2,7 +2,7 @@ function Show-IcingaForWindowsMenuRemoveComponents() { [array]$UninstallFeatures = @(); $AgentInstalled = (Get-IcingaAgentInstallation).Installed; - $PowerShellServiceInstalled = Get-Service -Name 'icingapowershell' -ErrorAction SilentlyContinue; + $PowerShellServiceInstalled = Get-IcingaWindowsServiceStatus -Service 'icingapowershell'; $IcingaWindowsServiceData = Get-IcingaForWindowsServiceData; $ModuleList = Get-Module 'icinga-powershell-*' -ListAvailable; @@ -45,7 +45,7 @@ function Show-IcingaForWindowsMenuRemoveComponents() 'Caption' = 'Uninstall Icinga for Windows Service'; 'Command' = 'Show-IcingaForWindowsMenuRemoveComponents'; 'Help' = 'This will remove the icingapowershell service for Icinga for Windows if installed' - 'Disabled' = ($null -eq $PowerShellServiceInstalled); + 'Disabled' = (-Not $PowerShellServiceInstalled.Present); 'Action' = @{ 'Command' = 'Show-IcingaWindowsManagementConsoleYesNoDialog'; 'Arguments' = @{ diff --git a/lib/core/installer/menu/manage/settings/services/ManageServices.psm1 b/lib/core/installer/menu/manage/settings/services/ManageServices.psm1 index 02d9f860..56d7c8ae 100644 --- a/lib/core/installer/menu/manage/settings/services/ManageServices.psm1 +++ b/lib/core/installer/menu/manage/settings/services/ManageServices.psm1 @@ -1,15 +1,15 @@ function Show-IcingaForWindowsMenuManageIcingaForWindowsServices() { - $IcingaAgentService = Get-Service 'icinga2' -ErrorAction SilentlyContinue; + $IcingaAgentService = Get-IcingaWindowsServiceStatus -Service 'icinga2'; $IcingaAgentStatus = 'Not Installed'; - $IcingaForWindowsService = Get-Service 'icingapowershell' -ErrorAction SilentlyContinue; + $IcingaForWindowsService = Get-IcingaWindowsServiceStatus -Service 'icingapowershell'; $IcingaForWindowsStatus = 'Not Installed'; - if ($null -ne $IcingaAgentService) { + if ($IcingaAgentService.Present) { $IcingaAgentStatus = $IcingaAgentService.Status; } - if ($null -ne $IcingaForWindowsService) { + if ($IcingaForWindowsService.Present) { $IcingaForWindowsStatus = $IcingaForWindowsService.Status; } @@ -20,43 +20,43 @@ function Show-IcingaForWindowsMenuManageIcingaForWindowsServices() 'Caption' = 'Start Icinga Agent Service'; 'Command' = 'Show-IcingaForWindowsMenuManageIcingaForWindowsServices'; 'Help' = 'Allows you to start the Icinga Agent if the service is not running'; - 'Disabled' = ($null -eq $IcingaAgentService -Or $IcingaAgentStatus -eq 'Running'); + 'Disabled' = (-Not $IcingaAgentService.Present -Or $IcingaAgentStatus -eq 'Running'); 'DisabledReason' = 'The Icinga Agent service is either not installed or the service is already running'; 'AdminMenu' = $TRUE; 'Action' = @{ 'Command' = 'Start-IcingaService'; - 'Arguments' = @{ '-Service' = 'icinga2'; }; + 'Arguments' = @{ '-Service' = 'icinga2'; '-Force' = $TRUE; }; } }, @{ 'Caption' = 'Stop Icinga Agent Service'; 'Command' = 'Show-IcingaForWindowsMenuManageIcingaForWindowsServices'; 'Help' = 'Allows you to stop the Icinga Agent if the service is not running'; - 'Disabled' = ($null -eq $IcingaAgentService -Or $IcingaAgentStatus -ne 'Running'); + 'Disabled' = (-Not $IcingaAgentService.Present -Or $IcingaAgentStatus -ne 'Running'); 'DisabledReason' = 'The Icinga Agent service is either not installed or the service is not running'; 'AdminMenu' = $TRUE; 'Action' = @{ 'Command' = 'Stop-IcingaService'; - 'Arguments' = @{ '-Service' = 'icinga2'; }; + 'Arguments' = @{ '-Service' = 'icinga2'; '-Force' = $TRUE; }; } }, @{ 'Caption' = 'Restart Icinga Agent Service'; 'Command' = 'Show-IcingaForWindowsMenuManageIcingaForWindowsServices'; 'Help' = 'Allows you to restart the Icinga Agent if the service is installed'; - 'Disabled' = ($null -eq $IcingaAgentService); + 'Disabled' = (-Not $IcingaAgentService.Present); 'DisabledReason' = 'The Icinga Agent service is not installed'; 'AdminMenu' = $TRUE; 'Action' = @{ 'Command' = 'Restart-IcingaService'; - 'Arguments' = @{ '-Service' = 'icinga2'; }; + 'Arguments' = @{ '-Service' = 'icinga2'; '-Force' = $TRUE; }; } }, @{ 'Caption' = 'Repair Icinga Agent Service'; 'Command' = 'Show-IcingaForWindowsMenuManageIcingaForWindowsServices'; 'Help' = 'Allows to repair the Icinga Agent service in case it was removed or broke during installation/upgrade'; - 'Disabled' = ($null -ne $IcingaAgentService); + 'Disabled' = ($IcingaAgentService.Present); 'DisabledReason' = 'The Icinga Agent service is already present'; 'AdminMenu' = $TRUE; 'Action' = @{ @@ -67,19 +67,19 @@ function Show-IcingaForWindowsMenuManageIcingaForWindowsServices() 'Caption' = 'Start Icinga for Windows Service'; 'Command' = 'Show-IcingaForWindowsMenuManageIcingaForWindowsServices'; 'Help' = 'Allows you to start the Icinga for Windows Service if the service is not running'; - 'Disabled' = ($null -eq $IcingaForWindowsService -Or $IcingaForWindowsStatus -eq 'Running'); + 'Disabled' = (-Not $IcingaForWindowsService.Present -Or $IcingaForWindowsStatus -eq 'Running'); 'DisabledReason' = 'The Icinga for Windows service is either not installed or already running'; 'AdminMenu' = $TRUE; 'Action' = @{ 'Command' = 'Start-IcingaService'; - 'Arguments' = @{ '-Service' = 'icingapowershell'; }; + 'Arguments' = @{ '-Service' = 'icingapowershell'; '-Force' = $TRUE; }; } }, @{ 'Caption' = 'Stop Icinga for Windows Service'; 'Command' = 'Show-IcingaForWindowsMenuManageIcingaForWindowsServices'; 'Help' = 'Allows you to stop the Icinga for Windows Service if the service is not running'; - 'Disabled' = ($null -eq $IcingaForWindowsService -Or $IcingaForWindowsStatus -ne 'Running'); + 'Disabled' = (-Not $IcingaForWindowsService.Present -Or $IcingaForWindowsStatus -ne 'Running'); 'DisabledReason' = 'The Icinga for Windows service is either not installed or not running'; 'AdminMenu' = $TRUE; 'Action' = @{ @@ -90,7 +90,7 @@ function Show-IcingaForWindowsMenuManageIcingaForWindowsServices() 'Caption' = 'Restart Icinga for Windows Service'; 'Command' = 'Show-IcingaForWindowsMenuManageIcingaForWindowsServices'; 'Help' = 'Allows you to restart the Icinga for Windows Service if the service is installed'; - 'Disabled' = ($null -eq $IcingaForWindowsService); + 'Disabled' = (-Not $IcingaForWindowsService.Present); 'DisabledReason' = 'The Icinga for Windows service is not installed'; 'AdminMenu' = $TRUE; 'Action' = @{ @@ -101,7 +101,7 @@ function Show-IcingaForWindowsMenuManageIcingaForWindowsServices() 'Caption' = 'Enable recovery settings for services'; 'Command' = 'Show-IcingaForWindowsMenuManageIcingaForWindowsServices'; 'Help' = 'Enables automatic service recovery for the Icinga Agent and Icinga for Windows service, in case the server terminates itself because of errors'; - 'Disabled' = ($null -eq $IcingaForWindowsService -And $null -eq $IcingaAgentService); + 'Disabled' = (-Not $IcingaForWindowsService.Present -And -Not $IcingaAgentService.Present); 'DisabledReason' = 'Neither the Icinga Agent nor the Icinga for Windows service are installed'; 'AdminMenu' = $TRUE; 'Action' = @{ @@ -112,7 +112,7 @@ function Show-IcingaForWindowsMenuManageIcingaForWindowsServices() 'Caption' = 'Disable recovery settings for services'; 'Command' = 'Show-IcingaForWindowsMenuManageIcingaForWindowsServices'; 'Help' = 'Disables automatic service recovery for the Icinga Agent and Icinga for Windows service, in case the server terminates itself because of errors'; - 'Disabled' = ($null -eq $IcingaForWindowsService -And $null -eq $IcingaAgentService); + 'Disabled' = (-Not $IcingaForWindowsService.Present -And -Not $IcingaAgentService.Present); 'DisabledReason' = 'Neither the Icinga Agent nor the Icinga for Windows service are installed'; 'AdminMenu' = $TRUE; 'Action' = @{ diff --git a/lib/core/installer/menu/manage/settings/troubleshooting/Troubleshooting.psm1 b/lib/core/installer/menu/manage/settings/troubleshooting/Troubleshooting.psm1 index e55bac0e..57de9ddb 100644 --- a/lib/core/installer/menu/manage/settings/troubleshooting/Troubleshooting.psm1 +++ b/lib/core/installer/menu/manage/settings/troubleshooting/Troubleshooting.psm1 @@ -1,6 +1,6 @@ function Show-IcingaForWindowsMenuManageTroubleshooting() { - $IcingaAgentService = Get-Service 'icinga2' -ErrorAction SilentlyContinue; + $IcingaAgentService = Get-IcingaWindowsServiceStatus -Service 'icinga2'; Show-IcingaForWindowsInstallerMenu ` -Header 'Troubleshooting options for problems:' ` @@ -45,7 +45,7 @@ function Show-IcingaForWindowsMenuManageTroubleshooting() 'Caption' = 'Repair Icinga Agent service'; 'Command' = 'Show-IcingaForWindowsMenuManageTroubleshooting'; 'Help' = 'Allows to repair the Icinga Agent service in case it was removed or broke during installation/upgrade'; - 'Disabled' = ($null -ne $IcingaAgentService); + 'Disabled' = (-Not $IcingaAgentService.Present); 'DisabledReason' = 'The Icinga Agent service is already present'; 'AdminMenu' = $TRUE; 'Action' = @{ diff --git a/lib/core/jea/Install-IcingaJeaProfile.psm1 b/lib/core/jea/Install-IcingaJeaProfile.psm1 index 80eb5305..6fb103f2 100644 --- a/lib/core/jea/Install-IcingaJeaProfile.psm1 +++ b/lib/core/jea/Install-IcingaJeaProfile.psm1 @@ -1,7 +1,7 @@ function Install-IcingaJEAProfile() { param ( - [string]$IcingaUser = ((Get-IcingaServices).icinga2.configuration.ServiceUser), + [string]$IcingaUser = (Get-IcingaServiceUser), [switch]$ConstrainedLanguage = $FALSE, [switch]$TestEnv = $FALSE, [switch]$RebuildFramework = $FALSE, diff --git a/lib/core/jea/Register-IcingaJEAProfile.psm1 b/lib/core/jea/Register-IcingaJEAProfile.psm1 index a9bfcb8b..09d8a5e9 100644 --- a/lib/core/jea/Register-IcingaJEAProfile.psm1 +++ b/lib/core/jea/Register-IcingaJEAProfile.psm1 @@ -1,7 +1,7 @@ function Register-IcingaJEAProfile() { param ( - [string]$IcingaUser = ((Get-IcingaServices).icinga2.configuration.ServiceUser), + [string]$IcingaUser = (Get-IcingaServiceUser), [switch]$ConstrainedLanguage = $FALSE, [switch]$TestEnv = $FALSE ); diff --git a/lib/core/repository/Install-IcingaComponent.psm1 b/lib/core/repository/Install-IcingaComponent.psm1 index 4e3b9840..53b87025 100644 --- a/lib/core/repository/Install-IcingaComponent.psm1 +++ b/lib/core/repository/Install-IcingaComponent.psm1 @@ -152,8 +152,8 @@ function Install-IcingaComponent() # These update steps only apply for the framework if ($Name.ToLower() -eq 'framework') { Remove-IcingaFrameworkDependencyFile; - $ServiceStatus = (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue).Status; - $AgentStatus = (Get-Service 'icinga2' -ErrorAction SilentlyContinue).Status; + $ServiceStatus = (Get-IcingaWindowsServiceStatus -Service 'icingapowershell').Status; + $AgentStatus = (Get-IcingaWindowsServiceStatus -Service 'icinga2').Status; if ($ServiceStatus -eq 'Running') { Write-IcingaConsoleNotice 'Stopping Icinga for Windows service'; @@ -351,12 +351,7 @@ function Install-IcingaComponent() } } - $MSIData = & powershell.exe -Command { - Use-Icinga -Minimal; - - $DownloadDestination = $args[0]; - return (Read-IcingaMSIMetadata -File $DownloadDestination); - } -Args $DownloadDestination; + $MSIData = Invoke-IcingaWindowsScheduledTask -JobType ReadMSIPackage -FilePath $DownloadDestination; if ($InstalledVersion.Full -eq $MSIData.ProductVersion -And $Force -eq $FALSE) { Write-IcingaConsoleWarning 'The package "agent" with version "{0}" is already installed. Use "-Force" to re-install the component' -Objects $InstalledVersion.Full; @@ -373,18 +368,7 @@ function Install-IcingaComponent() } } - $InstallProcess = & powershell.exe -Command { - Use-Icinga -Minimal; - - $DownloadDestination = $args[0]; - $InstallTarget = $args[1]; - $InstallProcess = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('/quiet /norestart /i "{0}" {1}', $DownloadDestination, $InstallTarget)) -FlushNewLines; - - Start-Sleep -Seconds 2; - Optimize-IcingaForWindowsMemory; - - return $InstallProcess; - } -Args $DownloadDestination, $InstallTarget; + $InstallProcess = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('/quiet /i "{0}" {1}', $DownloadDestination, $InstallTarget)) -FlushNewLines; if ($InstallProcess.ExitCode -ne 0) { Write-IcingaConsoleError -Message 'Failed to install component "agent": {0}{1}' -Objects $InstallProcess.Message, $InstallProcess.Error; diff --git a/lib/core/windows/Disable-IcingaServiceRecovery.psm1 b/lib/core/windows/Disable-IcingaServiceRecovery.psm1 index 678c5bf2..11afe01d 100644 --- a/lib/core/windows/Disable-IcingaServiceRecovery.psm1 +++ b/lib/core/windows/Disable-IcingaServiceRecovery.psm1 @@ -1,6 +1,6 @@ function Disable-IcingaServiceRecovery() { - if ($null -ne (Get-Service 'icinga2' -ErrorAction SilentlyContinue)) { + if ((Get-IcingaWindowsServiceStatus -Service 'icinga2').Present) { $ServiceStatus = Start-IcingaProcess -Executable 'sc.exe' -Arguments 'failure icinga2 reset=0 actions=none/0/none/0/none/0'; if ($ServiceStatus.ExitCode -ne 0) { @@ -10,7 +10,7 @@ function Disable-IcingaServiceRecovery() } } - if ($null -ne (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue)) { + if ((Get-IcingaWindowsServiceStatus -Service 'icingapowershell').Present) { $ServiceStatus = Start-IcingaProcess -Executable 'sc.exe' -Arguments 'failure icingapowershell reset=0 actions=none/0/none/0/none/0'; if ($ServiceStatus.ExitCode -ne 0) { diff --git a/lib/core/windows/Enable-IcingaServiceRecovery.psm1 b/lib/core/windows/Enable-IcingaServiceRecovery.psm1 index 94c4c6dc..aecdeb06 100644 --- a/lib/core/windows/Enable-IcingaServiceRecovery.psm1 +++ b/lib/core/windows/Enable-IcingaServiceRecovery.psm1 @@ -1,6 +1,6 @@ function Enable-IcingaServiceRecovery() { - if ($null -ne (Get-Service 'icinga2' -ErrorAction SilentlyContinue)) { + if ((Get-IcingaWindowsServiceStatus -Service 'icinga2').Present) { $ServiceStatus = Start-IcingaProcess -Executable 'sc.exe' -Arguments 'failure icinga2 reset=0 actions=restart/0/restart/0/restart/0'; if ($ServiceStatus.ExitCode -ne 0) { @@ -10,7 +10,7 @@ function Enable-IcingaServiceRecovery() } } - if ($null -ne (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue)) { + if ((Get-IcingaWindowsServiceStatus -Service 'icingapowershell').Present) { $ServiceStatus = Start-IcingaProcess -Executable 'sc.exe' -Arguments 'failure icingapowershell reset=0 actions=restart/0/restart/0/restart/0'; if ($ServiceStatus.ExitCode -ne 0) { diff --git a/lib/core/windows/Install-IcingaSecurity.psm1 b/lib/core/windows/Install-IcingaSecurity.psm1 index b19b115a..a2945ea4 100644 --- a/lib/core/windows/Install-IcingaSecurity.psm1 +++ b/lib/core/windows/Install-IcingaSecurity.psm1 @@ -4,7 +4,8 @@ function Install-IcingaSecurity() [string]$IcingaUser = 'icinga', [switch]$RebuildFramework = $FALSE, [switch]$AllowScriptBlocks = $FALSE, - [switch]$ConstrainedLanguage = $FALSE + [switch]$ConstrainedLanguage = $FALSE, + [switch]$RunAsTask = $FALSE ); if ($PSVersionTable.PSVersion -lt (New-IcingaVersionObject -Version 5, 0)) { @@ -27,7 +28,12 @@ function Install-IcingaSecurity() } Install-IcingaServiceUser -IcingaUser $IcingaUser; - Install-IcingaJEAProfile -IcingaUser $IcingaUser -RebuildFramework:$RebuildFramework -AllowScriptBlocks:$AllowScriptBlocks -ConstrainedLanguage:$ConstrainedLanguage; + + if ($RunAsTask) { + Invoke-IcingaWindowsScheduledTask -JobType InstallJEA -Timeout 600 | Out-Null; + } else { + Install-IcingaJEAProfile -IcingaUser $IcingaUser -RebuildFramework:$RebuildFramework -AllowScriptBlocks:$AllowScriptBlocks -ConstrainedLanguage:$ConstrainedLanguage; + } Restart-IcingaForWindows; } diff --git a/lib/core/windows/Restart-IcingaWindowsService.psm1 b/lib/core/windows/Restart-IcingaWindowsService.psm1 index cf448e53..ada4b786 100644 --- a/lib/core/windows/Restart-IcingaWindowsService.psm1 +++ b/lib/core/windows/Restart-IcingaWindowsService.psm1 @@ -1,14 +1,22 @@ function Restart-IcingaForWindows() { + param ( + [switch]$Force = $FALSE + ); + + if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) { + return; + } + [string]$JeaPid = Get-IcingaJEAServicePid; - Stop-IcingaService -Service 'icingapowershell'; + Stop-IcingaService -Service 'icingapowershell' -Force:$Force; if ((Test-IcingaJEAServiceRunning -JeaPid $JeaPid)) { Stop-IcingaJEAProcess -JeaPid $JeaPid; } - Restart-IcingaService -Service 'icingapowershell'; + Restart-IcingaService -Service 'icingapowershell' -Force:$Force; } Set-Alias -Name 'Restart-IcingaWindowsService' -Value 'Restart-IcingaForWindows'; diff --git a/lib/core/windows/Uninstall-IcingaServiceUser.psm1 b/lib/core/windows/Uninstall-IcingaServiceUser.psm1 index 3d1ce162..12b18e65 100644 --- a/lib/core/windows/Uninstall-IcingaServiceUser.psm1 +++ b/lib/core/windows/Uninstall-IcingaServiceUser.psm1 @@ -11,8 +11,8 @@ function Uninstall-IcingaServiceUser() Write-IcingaConsoleNotice 'Uninstalling user "{0}"' -Objects $IcingaUser; - Stop-IcingaService 'icinga2'; - Stop-IcingaWindowsService; + Stop-IcingaService 'icinga2' -Force; + Stop-IcingaWindowsService -Force; Set-IcingaPowerShellConfig -Path 'Framework.Icinga.ServiceUser' -Value '';