Skip to content

Commit f0abc36

Browse files
committed
Fixes missing environment service information on some systems
1 parent b5b96d7 commit f0abc36

File tree

3 files changed

+64
-14
lines changed

3 files changed

+64
-14
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1919

2020
* [#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
2121
* [#710](https://github.com/Icinga/icinga-powershell-framework/pull/710) Fixes various console errors while running Icinga for Windows outside of an administrative shell
22+
* [#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
2223

2324
## 1.12.0 (2024-03-26)
2425

lib/core/framework/New-IcingaEnvironmentVariable.psm1

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,23 @@ function New-IcingaEnvironmentVariable()
7171
$Global:Icinga.Protected.Add('GarbageCollector', @{ });
7272
$Global:Icinga.Protected.Add(
7373
'Environment', @{
74-
'Icinga Service' = $null;
75-
'PowerShell Service' = $null;
74+
'Icinga Service' = @{
75+
'Status' = '';
76+
'Present' = $FALSE;
77+
'Name' = 'icinga2';
78+
'DisplayName' = 'icinga2';
79+
'User' = 'NT Authority\NetworkService';
80+
'ServicePath' = '';
81+
};
82+
'PowerShell Service' = @{
83+
'Status' = '';
84+
'Present' = $FALSE;
85+
'Name' = 'icingapowershell';
86+
'DisplayName' = 'icingapowershell';
87+
'User' = 'NT Authority\NetworkService';
88+
'ServicePath' = '';
89+
};
90+
'FetchedServices' = $FALSE;
7691
}
7792
);
7893
}

lib/core/icingaagent/setters/Set-IcingaServiceEnvironment.psm1

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,11 @@ function Set-IcingaServiceEnvironment()
44
[switch]$Force = $FALSE
55
);
66

7-
if ($null -ne $Global:Icinga.Protected.Environment.'Icinga Service' -And $null -ne $Global:Icinga.Protected.Environment.'PowerShell Service' -And $Force -eq $FALSE) {
8-
return;
9-
}
10-
117
# Don't do anything if we are not inside an administrative shell
128
if ((Test-AdministrativeShell) -eq $FALSE) {
139
return;
1410
}
1511

16-
# Use scheduled tasks to fetch our current service configuration for faster load times afterwards
17-
$IcingaService = Invoke-IcingaWindowsScheduledTask -JobType GetWindowsService -ObjectName 'icinga2';
18-
$PowerShellService = Invoke-IcingaWindowsScheduledTask -JobType GetWindowsService -ObjectName 'icingapowershell';
19-
2012
# Ensure we build our internal environment variables based on each version
2113
# This is just required to prevent possible issues during upgrades from one version to another
2214
if ($Global:Icinga.Protected.ContainsKey('Environment') -eq $FALSE) {
@@ -26,22 +18,62 @@ function Set-IcingaServiceEnvironment()
2618
);
2719
}
2820

21+
if ($Global:Icinga.Protected.ContainsKey('Environment') -eq $FALSE) {
22+
$Global:Icinga.Protected.Add(
23+
'Environment',
24+
@{ }
25+
);
26+
}
27+
28+
if ($Global:Icinga.Protected.Environment.ContainsKey('FetchedServices') -eq $FALSE) {
29+
$Global:Icinga.Protected.Environment.Add(
30+
'FetchedServices', $FALSE
31+
);
32+
}
33+
2934
if ($Global:Icinga.Protected.Environment.ContainsKey('Icinga Service') -eq $FALSE) {
3035
$Global:Icinga.Protected.Environment.Add(
3136
'Icinga Service',
32-
$null
37+
@{
38+
'Status' = '';
39+
'Present' = $FALSE;
40+
'Name' = 'icinga2';
41+
'DisplayName' = 'icinga2';
42+
'User' = 'NT Authority\NetworkService';
43+
'ServicePath' = '';
44+
}
3345
);
3446
}
3547

3648
if ($Global:Icinga.Protected.Environment.ContainsKey('PowerShell Service') -eq $FALSE) {
3749
$Global:Icinga.Protected.Environment.Add(
3850
'PowerShell Service',
39-
$null
51+
@{
52+
'Status' = '';
53+
'Present' = $FALSE;
54+
'Name' = 'icingapowershell';
55+
'DisplayName' = 'icingapowershell';
56+
'User' = 'NT Authority\NetworkService';
57+
'ServicePath' = '';
58+
}
4059
);
4160
}
4261

43-
$Global:Icinga.Protected.Environment.'Icinga Service' = $IcingaService.Service;
44-
$Global:Icinga.Protected.Environment.'PowerShell Service' = $PowerShellService.Service;
62+
if ($Global:Icinga.Protected.Environment.FetchedServices) {
63+
return;
64+
}
65+
66+
# Use scheduled tasks to fetch our current service configuration for faster load times afterwards
67+
$IcingaService = Invoke-IcingaWindowsScheduledTask -JobType GetWindowsService -ObjectName 'icinga2';
68+
$PowerShellService = Invoke-IcingaWindowsScheduledTask -JobType GetWindowsService -ObjectName 'icingapowershell';
69+
70+
if ($null -ne $IcingaService -And $null -ne $IcingaService.Service) {
71+
$Global:Icinga.Protected.Environment.'Icinga Service' = $IcingaService.Service;
72+
}
73+
74+
if ($null -ne $PowerShellService -And $null -ne $PowerShellService.Service) {
75+
$Global:Icinga.Protected.Environment.'PowerShell Service' = $PowerShellService.Service;
76+
}
4577

4678
# In case the services are not present, ensure defaults are always set
4779
if ($Global:Icinga.Protected.Environment.'Icinga Service'.User -eq 'Unknown') {
@@ -50,4 +82,6 @@ function Set-IcingaServiceEnvironment()
5082
if ($Global:Icinga.Protected.Environment.'PowerShell Service'.User -eq 'Unknown') {
5183
$Global:Icinga.Protected.Environment.'PowerShell Service'.User = 'NT Authority\NetworkService';
5284
}
85+
86+
$Global:Icinga.Protected.Environment.FetchedServices = $TRUE;
5387
}

0 commit comments

Comments
 (0)