diff --git a/doc/100-General/10-Changelog.md b/doc/100-General/10-Changelog.md index 58574c4..431a3df 100644 --- a/doc/100-General/10-Changelog.md +++ b/doc/100-General/10-Changelog.md @@ -17,6 +17,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic * [#707](https://github.com/Icinga/icinga-powershell-framework/pull/707) Fixes size of the `Icinga for Windows` eventlog by setting it to `20MiB`, allowing to store more events before they are overwritten * [#710](https://github.com/Icinga/icinga-powershell-framework/pull/710) Fixes various console errors while running Icinga for Windows outside of an administrative shell +* [#714](https://github.com/Icinga/icinga-powershell-framework/pull/714) Fixes missing service environment information during initial setup of Icinga for Windows v1.12 on some systems ## 1.12.0 (2024-03-26) diff --git a/lib/core/framework/New-IcingaEnvironmentVariable.psm1 b/lib/core/framework/New-IcingaEnvironmentVariable.psm1 index 58c64c1..2355ec9 100644 --- a/lib/core/framework/New-IcingaEnvironmentVariable.psm1 +++ b/lib/core/framework/New-IcingaEnvironmentVariable.psm1 @@ -71,8 +71,23 @@ function New-IcingaEnvironmentVariable() $Global:Icinga.Protected.Add('GarbageCollector', @{ }); $Global:Icinga.Protected.Add( 'Environment', @{ - 'Icinga Service' = $null; - 'PowerShell Service' = $null; + 'Icinga Service' = @{ + 'Status' = ''; + 'Present' = $FALSE; + 'Name' = 'icinga2'; + 'DisplayName' = 'icinga2'; + 'User' = 'NT Authority\NetworkService'; + 'ServicePath' = ''; + }; + 'PowerShell Service' = @{ + 'Status' = ''; + 'Present' = $FALSE; + 'Name' = 'icingapowershell'; + 'DisplayName' = 'icingapowershell'; + 'User' = 'NT Authority\NetworkService'; + 'ServicePath' = ''; + }; + 'FetchedServices' = $FALSE; } ); } diff --git a/lib/core/icingaagent/setters/Set-IcingaServiceEnvironment.psm1 b/lib/core/icingaagent/setters/Set-IcingaServiceEnvironment.psm1 index e009d53..52c8fed 100644 --- a/lib/core/icingaagent/setters/Set-IcingaServiceEnvironment.psm1 +++ b/lib/core/icingaagent/setters/Set-IcingaServiceEnvironment.psm1 @@ -4,19 +4,11 @@ function Set-IcingaServiceEnvironment() [switch]$Force = $FALSE ); - if ($null -ne $Global:Icinga.Protected.Environment.'Icinga Service' -And $null -ne $Global:Icinga.Protected.Environment.'PowerShell Service' -And $Force -eq $FALSE) { - return; - } - # Don't do anything if we are not inside an administrative shell if ((Test-AdministrativeShell) -eq $FALSE) { return; } - # Use scheduled tasks to fetch our current service configuration for faster load times afterwards - $IcingaService = Invoke-IcingaWindowsScheduledTask -JobType GetWindowsService -ObjectName 'icinga2'; - $PowerShellService = Invoke-IcingaWindowsScheduledTask -JobType GetWindowsService -ObjectName 'icingapowershell'; - # Ensure we build our internal environment variables based on each version # This is just required to prevent possible issues during upgrades from one version to another if ($Global:Icinga.Protected.ContainsKey('Environment') -eq $FALSE) { @@ -26,22 +18,62 @@ function Set-IcingaServiceEnvironment() ); } + if ($Global:Icinga.Protected.ContainsKey('Environment') -eq $FALSE) { + $Global:Icinga.Protected.Add( + 'Environment', + @{ } + ); + } + + if ($Global:Icinga.Protected.Environment.ContainsKey('FetchedServices') -eq $FALSE) { + $Global:Icinga.Protected.Environment.Add( + 'FetchedServices', $FALSE + ); + } + if ($Global:Icinga.Protected.Environment.ContainsKey('Icinga Service') -eq $FALSE) { $Global:Icinga.Protected.Environment.Add( 'Icinga Service', - $null + @{ + 'Status' = ''; + 'Present' = $FALSE; + 'Name' = 'icinga2'; + 'DisplayName' = 'icinga2'; + 'User' = 'NT Authority\NetworkService'; + 'ServicePath' = ''; + } ); } if ($Global:Icinga.Protected.Environment.ContainsKey('PowerShell Service') -eq $FALSE) { $Global:Icinga.Protected.Environment.Add( 'PowerShell Service', - $null + @{ + 'Status' = ''; + 'Present' = $FALSE; + 'Name' = 'icingapowershell'; + 'DisplayName' = 'icingapowershell'; + 'User' = 'NT Authority\NetworkService'; + 'ServicePath' = ''; + } ); } - $Global:Icinga.Protected.Environment.'Icinga Service' = $IcingaService.Service; - $Global:Icinga.Protected.Environment.'PowerShell Service' = $PowerShellService.Service; + if ($Global:Icinga.Protected.Environment.FetchedServices) { + return; + } + + # Use scheduled tasks to fetch our current service configuration for faster load times afterwards + $IcingaService = Invoke-IcingaWindowsScheduledTask -JobType GetWindowsService -ObjectName 'icinga2'; + $PowerShellService = Invoke-IcingaWindowsScheduledTask -JobType GetWindowsService -ObjectName 'icingapowershell'; + + if ($null -ne $IcingaService -And $null -ne $IcingaService.Service) { + $Global:Icinga.Protected.Environment.'Icinga Service' = $IcingaService.Service; + } + + if ($null -ne $PowerShellService -And $null -ne $PowerShellService.Service) { + $Global:Icinga.Protected.Environment.'PowerShell Service' = $PowerShellService.Service; + } # In case the services are not present, ensure defaults are always set if ($Global:Icinga.Protected.Environment.'Icinga Service'.User -eq 'Unknown') { @@ -50,4 +82,6 @@ function Set-IcingaServiceEnvironment() if ($Global:Icinga.Protected.Environment.'PowerShell Service'.User -eq 'Unknown') { $Global:Icinga.Protected.Environment.'PowerShell Service'.User = 'NT Authority\NetworkService'; } + + $Global:Icinga.Protected.Environment.FetchedServices = $TRUE; }