Skip to content

Commit 5ca2629

Browse files
authored
Merge pull request #61 from Icinga/fix/windows_update_permission_exception
Adds exception handling for win updates permission
2 parents 16161b5 + 5a42ef4 commit 5ca2629

File tree

3 files changed

+78
-68
lines changed

3 files changed

+78
-68
lines changed

doc/31-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This release adds the following new plugin:
1919

2020
### Enhancements
2121

22+
* [#61](https://github.com/Icinga/icinga-powershell-plugins/issues/61) Adds exception handling in case permissions to access Windows Updates are missing on the system
2223
* [#73](https://github.com/Icinga/icinga-powershell-plugins/issues/73) Improves plugin creation Cmdlet to write a new permission section and to create the `plugins` doc folder in case it does not exist
2324
* [#74](https://github.com/Icinga/icinga-powershell-plugins/pull/74) Adds `avg. disk queue length` metric for monitoring and performance data to `Invoke-IcingaCheckDiskHealth`
2425
* [#78](https://github.com/Icinga/icinga-powershell-plugins/issues/78) Improves the documentation and output for `Invoke-IcingaCheckService` by adding metrics for all found services configured to run `Automatic` and adds service output on Verbosity 1 to show a list of all found services including their current state
Lines changed: 70 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,79 @@
11
function Get-IcingaUpdatesInstalled ()
22
{
33

4-
# Fetch all informations about installed updates and add them
5-
$WindowsUpdates = New-Object -ComObject "Microsoft.Update.Session";
6-
$SearchIndex = $WindowsUpdates.CreateUpdateSearcher();
7-
[hashtable]$UpdateList = @{};
8-
[hashtable]$UpdateInstalled = @{};
9-
[hashtable]$UpdateUninstalled = @{};
10-
[hashtable]$UpdateOther = @{};
4+
# Fetch all informations about installed updates and add them
5+
try {
6+
$WindowsUpdates = New-Object -ComObject "Microsoft.Update.Session" -ErrorAction Stop;
7+
$SearchIndex = $WindowsUpdates.CreateUpdateSearcher();
8+
} catch {
9+
Exit-IcingaThrowException -ExceptionType 'Permission' -ExceptionThrown $IcingaExceptions.Permission.WindowsUpdate -Force;
10+
}
1111

12-
# Operation ID's
13-
# 1: Installed
14-
# 2: Uninstalled
15-
# 3: Other
12+
[hashtable]$UpdateList = @{};
13+
[hashtable]$UpdateInstalled = @{};
14+
[hashtable]$UpdateUninstalled = @{};
15+
[hashtable]$UpdateOther = @{};
1616

17-
# At first get a list of our Windows Update history
18-
$Updates = $SearchIndex.QueryHistory(0, $SearchIndex.GetTotalHistoryCount()) |
19-
Select-Object Operation, ResultCode, HResult, Date, Title, Description, ServiceID, SupportUrl;
17+
# Operation ID's
18+
# 1: Installed
19+
# 2: Uninstalled
20+
# 3: Other
2021

21-
foreach ($update in $Updates) {
22-
[string]$UpdateKey = [string]::Format('{0} [{1}|{2}]', $update.Title, $update.Date, $update.HResult);
23-
switch ($update.Operation) {
24-
1 {
25-
if ($UpdateInstalled.ContainsKey($UpdateKey) -eq $FALSE) {
26-
$UpdateInstalled.Add($UpdateKey, $update);
27-
} else {
28-
$Icinga2.Log.Write(
29-
$Icinga2.Enums.LogState.Warning,
30-
[string]::Format(
31-
'Unable to add update "{0}" to update list. The key with content "{1}" is already present',
32-
$UpdateKey,
33-
$update
34-
)
35-
);
36-
}
37-
};
38-
2 {
39-
if ($UpdateUninstalled.ContainsKey($UpdateKey) -eq $FALSE) {
40-
$UpdateUninstalled.Add($UpdateKey, $update);
41-
} else {
42-
$Icinga2.Log.Write(
43-
$Icinga2.Enums.LogState.Warning,
44-
[string]::Format(
45-
'Unable to add update "{0}" to update list. The key with content "{1}" is already present',
46-
$UpdateKey,
47-
$update
48-
)
49-
);
50-
}
51-
};
52-
default {
53-
if ($UpdateOther.ContainsKey($UpdateKey) -eq $FALSE) {
54-
$UpdateOther.Add($UpdateKey, $update);
55-
} else {
56-
$Icinga2.Log.Write(
57-
$Icinga2.Enums.LogState.Warning,
58-
[string]::Format(
59-
'Unable to add update "{0}" to update list. The key with content "{1}" is already present',
60-
$UpdateKey,
61-
$update
62-
)
63-
);
64-
}
65-
};
66-
}
67-
}
22+
# At first get a list of our Windows Update history
23+
$Updates = $SearchIndex.QueryHistory(0, $SearchIndex.GetTotalHistoryCount()) |
24+
Select-Object Operation, ResultCode, HResult, Date, Title, Description, ServiceID, SupportUrl;
6825

69-
$UpdateList.Add('installed', $UpdateInstalled);
70-
$UpdateList.Add('uninstalled', $UpdateUninstalled);
71-
$UpdateList.Add('other', $UpdateOther);
26+
foreach ($update in $Updates) {
27+
[string]$UpdateKey = [string]::Format('{0} [{1}|{2}]', $update.Title, $update.Date, $update.HResult);
28+
switch ($update.Operation) {
29+
1 {
30+
if ($UpdateInstalled.ContainsKey($UpdateKey) -eq $FALSE) {
31+
$UpdateInstalled.Add($UpdateKey, $update);
32+
} else {
33+
$Icinga2.Log.Write(
34+
$Icinga2.Enums.LogState.Warning,
35+
[string]::Format(
36+
'Unable to add update "{0}" to update list. The key with content "{1}" is already present',
37+
$UpdateKey,
38+
$update
39+
)
40+
);
41+
}
42+
};
43+
2 {
44+
if ($UpdateUninstalled.ContainsKey($UpdateKey) -eq $FALSE) {
45+
$UpdateUninstalled.Add($UpdateKey, $update);
46+
} else {
47+
$Icinga2.Log.Write(
48+
$Icinga2.Enums.LogState.Warning,
49+
[string]::Format(
50+
'Unable to add update "{0}" to update list. The key with content "{1}" is already present',
51+
$UpdateKey,
52+
$update
53+
)
54+
);
55+
}
56+
};
57+
default {
58+
if ($UpdateOther.ContainsKey($UpdateKey) -eq $FALSE) {
59+
$UpdateOther.Add($UpdateKey, $update);
60+
} else {
61+
$Icinga2.Log.Write(
62+
$Icinga2.Enums.LogState.Warning,
63+
[string]::Format(
64+
'Unable to add update "{0}" to update list. The key with content "{1}" is already present',
65+
$UpdateKey,
66+
$update
67+
)
68+
);
69+
}
70+
};
71+
}
72+
}
7273

73-
return $UpdateList;
74+
$UpdateList.Add('installed', $UpdateInstalled);
75+
$UpdateList.Add('uninstalled', $UpdateUninstalled);
76+
$UpdateList.Add('other', $UpdateOther);
7477

75-
}
78+
return $UpdateList;
79+
}

provider/updates/Get-IcingaUpdatesPending.psm1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ function Get-IcingaUpdatesPending ()
33

44
[hashtable]$PendingUpdates = @{};
55
[hashtable]$PendingUpdateNameCache = @{};
6+
67
# Fetch all informations about installed updates and add them
7-
$WindowsUpdates = New-Object -ComObject "Microsoft.Update.Session";
8-
$SearchIndex = $WindowsUpdates.CreateUpdateSearcher();
8+
try {
9+
$WindowsUpdates = New-Object -ComObject "Microsoft.Update.Session" -ErrorAction Stop;
10+
$SearchIndex = $WindowsUpdates.CreateUpdateSearcher();
11+
} catch {
12+
Exit-IcingaThrowException -ExceptionType 'Permission' -ExceptionThrown $IcingaExceptions.Permission.WindowsUpdate -Force;
13+
}
914

1015
try {
1116
# Get a list of current pending updates which are not yet installed on the system

0 commit comments

Comments
 (0)