From ac459d7fda2348852bb93276cb711dbf5fac9d16 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Wed, 18 Sep 2019 12:42:20 +0200 Subject: [PATCH] Added additional exception handling for EventLog Check --- lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1 | 3 ++- lib/provider/eventlog/Get-IcingaEventLog.psm1 | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1 b/lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1 index ae8c890..41b8cbb 100644 --- a/lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1 +++ b/lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1 @@ -11,7 +11,8 @@ [hashtable]$Inputs = @{ PerformanceCounter = 'A plugin failed to fetch Performance Counter information. Please ensure the counter is written properly and available on your system.'; - EventLog = 'Failed to fetch EventLog information. Please specify a LogName.'; + EventLogLogName = 'Failed to fetch EventLog information. Please specify a valid LogName.'; + EventLog = 'Failed to fetch EventLog information. Please check your inputs for EntryTypes and other categories and try again.'; }; <# diff --git a/lib/provider/eventlog/Get-IcingaEventLog.psm1 b/lib/provider/eventlog/Get-IcingaEventLog.psm1 index 487d759..87f734e 100644 --- a/lib/provider/eventlog/Get-IcingaEventLog.psm1 +++ b/lib/provider/eventlog/Get-IcingaEventLog.psm1 @@ -56,10 +56,15 @@ function Get-IcingaEventLog() $EventLogArguments.Add('Before', $Before); } - $events = Get-EventLog @EventLogArguments; + try { + $events = Get-EventLog @EventLogArguments; + } catch { + Exit-IcingaThrowException -InputString $_.Exception -StringPattern 'ParameterBindingValidationException' -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.EventLog; + Exit-IcingaThrowException -InputString $_.Exception -StringPattern 'System.InvalidOperationException' -CustomMessage (-Join $LogName) -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.EventLogLogName; + Exit-IcingaThrowException -InputString $_.Exception -ExceptionType 'Unhandled' -Force; + } if ($null -ne $ExcludeEventId -Or $null -ne $ExcludeUsername -Or $null -ne $ExcludeEntryType -Or $null -ne $ExcludeMessage) { - Write-Host 'Filtering started!' $filteredEvents = @(); foreach ($event in $events) { # Filter out excluded event IDs