Skip to content

Commit a72d109

Browse files
committed
Fixes PermissionDenied exception messages
1 parent 3d1aaac commit a72d109

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

doc/31-Changelog.md

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

1414
## Bugfixes
1515

16+
[#196](https://github.com/Icinga/icinga-powershell-plugins/issues/196) Fixes unhandled PermissionDenied exceptions for `Invoke-IcingaCheckDirectory`, in case required permissions for files and folders were not set
1617
[#202](https://github.com/Icinga/icinga-powershell-plugins/pull/202) Fixes `\` and `/` which are now removed again for `-Include` and `-Exclude` arrays on `Invoke-IcingaCheckUsedPartitionSpace`.
1718

1819
## 1.5.0 (2021-06-02)

provider/directory/Icinga_Provider_Directory.psm1

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,26 @@ function Get-IcingaDirectory()
7474
return @();
7575
}
7676

77-
$DirectoryData = Get-ChildItem -Path $Path |
78-
Where-Object {
79-
foreach ($element in $FileNames) {
80-
if ($_.Name -like $element) {
81-
return $_.Name
77+
try {
78+
$DirectoryData = Get-ChildItem -Path $Path -ErrorAction Stop |
79+
Where-Object {
80+
foreach ($element in $FileNames) {
81+
if ($_.Name -like $element) {
82+
return $_.Name
83+
}
8284
}
8385
}
84-
}
86+
} catch {
87+
$ExMsg = $_.Exception.Message;
88+
Write-IcingaConsoleNotice $_.CategoryInfo;
89+
Exit-IcingaThrowException `
90+
-InputString $_.CategoryInfo `
91+
-StringPattern 'PermissionDenied' `
92+
-ExceptionType 'Input' `
93+
-ExceptionThrown $IcingaPluginExceptions.FileSystem.PermissionDenied;
94+
95+
Exit-IcingaThrowException -ExceptionType 'Input' -CustomMessage 'Filesystem Exception' -ExceptionThrown $ExMsg -Force;
96+
}
8597

8698
return $DirectoryData;
8799
}
@@ -98,7 +110,18 @@ function Get-IcingaDirectoryRecurse()
98110
return @();
99111
}
100112

101-
$DirectoryData = Get-ChildItem -Recurse -Include $FileNames -Path $Path;
113+
try {
114+
$DirectoryData = Get-ChildItem -Recurse -Include $FileNames -Path $Path -ErrorAction Stop;
115+
} catch {
116+
$ExMsg = $_.Exception.Message;
117+
Exit-IcingaThrowException `
118+
-InputString $_.CategoryInfo `
119+
-StringPattern 'PermissionDenied' `
120+
-ExceptionType 'Input' `
121+
-ExceptionThrown $IcingaPluginExceptions.FileSystem.PermissionDenied;
122+
123+
Exit-IcingaThrowException -ExceptionType 'Input' -CustomMessage 'Filesystem Exception' -ExceptionThrown $ExMsg -Force;
124+
}
102125

103126
return $DirectoryData;
104127
}

provider/enums/IcingaGeneralPlugins_Exceptions.psm1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
EventLogNoMessageEntries = 'Failed to fetch EventLog information. The argument `-MaxEntries` requires to be greater or equal 1';
55
};
66

7+
[hashtable]$FileSystem = @{
8+
PermissionDenied = 'You are not permitted to access the defined path.';
9+
}
10+
711
if ($null -eq $IcingaPluginExceptions) {
812
[hashtable]$IcingaPluginExceptions = @{
9-
Inputs = $Inputs;
13+
Inputs = $Inputs;
14+
FileSystem = $FileSystem;
1015
}
1116
}
1217

0 commit comments

Comments
 (0)