Fixes missing environment service information on some systems

This commit is contained in:
Lord Hepipud 2024-04-02 20:43:53 +02:00
parent 45c4a2ba60
commit 807fd285e0
3 changed files with 64 additions and 14 deletions

View file

@ -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 * [#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 * [#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) ## 1.12.0 (2024-03-26)

View file

@ -71,8 +71,23 @@ function New-IcingaEnvironmentVariable()
$Global:Icinga.Protected.Add('GarbageCollector', @{ }); $Global:Icinga.Protected.Add('GarbageCollector', @{ });
$Global:Icinga.Protected.Add( $Global:Icinga.Protected.Add(
'Environment', @{ 'Environment', @{
'Icinga Service' = $null; 'Icinga Service' = @{
'PowerShell Service' = $null; '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;
} }
); );
} }

View file

@ -4,19 +4,11 @@ function Set-IcingaServiceEnvironment()
[switch]$Force = $FALSE [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 # Don't do anything if we are not inside an administrative shell
if ((Test-AdministrativeShell) -eq $FALSE) { if ((Test-AdministrativeShell) -eq $FALSE) {
return; 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 # 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 # This is just required to prevent possible issues during upgrades from one version to another
if ($Global:Icinga.Protected.ContainsKey('Environment') -eq $FALSE) { 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) { if ($Global:Icinga.Protected.Environment.ContainsKey('Icinga Service') -eq $FALSE) {
$Global:Icinga.Protected.Environment.Add( $Global:Icinga.Protected.Environment.Add(
'Icinga Service', '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) { if ($Global:Icinga.Protected.Environment.ContainsKey('PowerShell Service') -eq $FALSE) {
$Global:Icinga.Protected.Environment.Add( $Global:Icinga.Protected.Environment.Add(
'PowerShell Service', 'PowerShell Service',
$null @{
'Status' = '';
'Present' = $FALSE;
'Name' = 'icingapowershell';
'DisplayName' = 'icingapowershell';
'User' = 'NT Authority\NetworkService';
'ServicePath' = '';
}
); );
} }
$Global:Icinga.Protected.Environment.'Icinga Service' = $IcingaService.Service; if ($Global:Icinga.Protected.Environment.FetchedServices) {
$Global:Icinga.Protected.Environment.'PowerShell Service' = $PowerShellService.Service; 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 # In case the services are not present, ensure defaults are always set
if ($Global:Icinga.Protected.Environment.'Icinga Service'.User -eq 'Unknown') { 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') { if ($Global:Icinga.Protected.Environment.'PowerShell Service'.User -eq 'Unknown') {
$Global:Icinga.Protected.Environment.'PowerShell Service'.User = 'NT Authority\NetworkService'; $Global:Icinga.Protected.Environment.'PowerShell Service'.User = 'NT Authority\NetworkService';
} }
$Global:Icinga.Protected.Environment.FetchedServices = $TRUE;
} }