From 47272bd24397207c3afb56c8920cba7d31909eb8 Mon Sep 17 00:00:00 2001 From: Christian Stein Date: Thu, 6 Aug 2020 19:06:59 +0200 Subject: [PATCH] Fixes permission error on EventLog registration by printing proper error message Fixes #81 --- doc/31-Changelog.md | 2 +- icinga-powershell-framework.psm1 | 6 ++++-- lib/core/logging/Register-IcingaEventLog.psm1 | 10 ++++------ lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1 | 1 + 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/doc/31-Changelog.md b/doc/31-Changelog.md index 3feab10..4053092 100644 --- a/doc/31-Changelog.md +++ b/doc/31-Changelog.md @@ -36,7 +36,7 @@ Check Command configuration generated by Icinga for Windows 1.2.0 require Icinga * [#78](https://github.com/Icinga/icinga-powershell-framework/issues/78) Fix Icinga Agent package fetching for x86 architecture * [#79](https://github.com/Icinga/icinga-powershell-framework/issues/79) Fix ConvertTo-Seconds to output valid numeric data with multiple digits -* [#81](https://github.com/Icinga/icinga-powershell-framework/issues/81), [#82](https://github.com/Icinga/icinga-powershell-framework/issues/82) Fix error on EventLog initialising in case `Icinga for Windows` application is not registered on new machines +* [#81](https://github.com/Icinga/icinga-powershell-framework/issues/81), [#82](https://github.com/Icinga/icinga-powershell-framework/issues/82) Fix error on EventLog initialising in case `Icinga for Windows` application is not registered on new machines and throws proper error message on plugin execution on how to resolve it * [#84](https://github.com/Icinga/icinga-powershell-framework/issues/84), Fix conversion of `ConvertTo-Seconds` and `ConvertTo-SecondsFromIcingaThresholds` while the input value is `$null` * [#85](https://github.com/Icinga/icinga-powershell-framework/issues/85), Fix incorrect handling to empty service user password which was configured as empty `String` instead of `$null` `SecureString` object * [#89](https://github.com/Icinga/icinga-powershell-framework/issues/89), Fix file type question during `Get-IcingaCheckCommandConfig` generation in Windows 2012 R2 and older diff --git a/icinga-powershell-framework.psm1 b/icinga-powershell-framework.psm1 index 24e6277..d90d40b 100644 --- a/icinga-powershell-framework.psm1 +++ b/icinga-powershell-framework.psm1 @@ -29,8 +29,6 @@ function Use-Icinga() Import-IcingaLib '\' -Init; if ($LibOnly -eq $FALSE) { - Register-IcingaEventLog; - $global:IcingaThreads = [hashtable]::Synchronized(@{}); $global:IcingaThreadContent = [hashtable]::Synchronized(@{}); $global:IcingaThreadPool = [hashtable]::Synchronized(@{}); @@ -73,6 +71,10 @@ function Use-Icinga() -Value $entry[$event] | Out-Null; } } + + if ($LibOnly -eq $FALSE) { + Register-IcingaEventLog; + } } function Import-IcingaLib() diff --git a/lib/core/logging/Register-IcingaEventLog.psm1 b/lib/core/logging/Register-IcingaEventLog.psm1 index e1709e0..c6e69b6 100644 --- a/lib/core/logging/Register-IcingaEventLog.psm1 +++ b/lib/core/logging/Register-IcingaEventLog.psm1 @@ -12,11 +12,9 @@ function Register-IcingaEventLog() if ($Registered) { return; } - } catch { - # Nothing to handle here. We should simply register our application - # whtin the Application Event-Log. We will only run into this catch - # block if the app was not registered. - } - New-EventLog -LogName Application -Source 'Icinga for Windows'; + New-EventLog -LogName Application -Source 'Icinga for Windows'; + } catch { + Exit-IcingaThrowException -ExceptionType 'Configuration' -ExceptionThrown $IcingaExceptions.Configuration.EventLogNotInstalled -Force; + } } diff --git a/lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1 b/lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1 index 81924bc..3f17961 100644 --- a/lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1 +++ b/lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1 @@ -25,6 +25,7 @@ PluginArgumentMissing = 'Your plugin argument configuration is missing mandatory arguments. This is error is caused when mandatory or required arguments are missing from a plugin call and the operation is unable to process without them.'; PluginNotInstalled = 'The plugin assigned to this service check seems not to be installed on this machine. Please review your service check configuration for spelling errors and check if the plugin is installed and executable on this machine by PowerShell.'; PluginNotAssigned = 'Your check for this service could not be processed because it seems like no valid Cmdlet was assigned to the check command. Please review your check command to ensure that a valid Cmdlet is assigned and executed by a PowerShell call.'; + EventLogNotInstalled = 'Your Icinga PowerShell Framework has been executed by an unprivileged user before it was properly installed. The Windows EventLog application could not be registered because the current user has insufficient permissions. Please log into the machine and run "Use-Icinga" once from an administrative shell to complete the setup process. Once done this error should vanish.'; } <#