From 3eac2b30803e331ada1f1e0f52cd81934b323430 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Thu, 27 Jan 2022 19:50:30 +0100 Subject: [PATCH] Fixes error handling for failed module import --- doc/100-General/10-Changelog.md | 1 + lib/core/repository/Install-IcingaComponent.psm1 | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/doc/100-General/10-Changelog.md b/doc/100-General/10-Changelog.md index b755e3f..197e47e 100644 --- a/doc/100-General/10-Changelog.md +++ b/doc/100-General/10-Changelog.md @@ -31,6 +31,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic * [#439](https://github.com/Icinga/icinga-powershell-framework/pull/439) Moves PerformanceCounter to private space from previous public, which caused some problems * [#441](https://github.com/Icinga/icinga-powershell-framework/pull/441) Fixes an exception while loading the Framework, caused by a race condition for missing environment variables which are accessed by some plugins before the Framework is loaded properly * [#446](https://github.com/Icinga/icinga-powershell-framework/pull/446) Fixes Icinga for Windows progress preference, which sometimes caused UI glitches +* [#449](https://github.com/Icinga/icinga-powershell-framework/pull/449) Fixes unhandled exception while importing modules during `Install-IcingaComponent` process, because of possible missing dependencies ### Enhancements diff --git a/lib/core/repository/Install-IcingaComponent.psm1 b/lib/core/repository/Install-IcingaComponent.psm1 index a303953..bb161b2 100644 --- a/lib/core/repository/Install-IcingaComponent.psm1 +++ b/lib/core/repository/Install-IcingaComponent.psm1 @@ -202,16 +202,20 @@ function Install-IcingaComponent() Start-IcingaService 'icinga2'; } } else { - Import-Module -Name $ComponentFolder -Force; - Import-Module -Name $ComponentFolder -Force -Global; + try { + Import-Module -Name $ComponentFolder -Force -ErrorAction Stop; + Import-Module -Name $ComponentFolder -Force -Global -ErrorAction Stop; + + Write-IcingaConsoleNotice 'Installation of component "{0}" with version "{1}" was successful. Open a new PowerShell to apply the changes' -Objects $Name.ToLower(), $ManifestFile.ModuleVersion; + } catch { + Write-IcingaConsoleError 'Component "{0}" has been installed with version "{1}", but while importing the component an exception was thrown: {2}' -Objects $Name.ToLower(), $ManifestFile.ModuleVersion, $_.Exception.Message; + } } # This will ensure that Framework functions will always win over third party functions, overwriting functionality # of the Framework, which might cause problems during installation otherwise Import-Module (Join-Path -Path (Get-IcingaForWindowsRootPath) -ChildPath 'icinga-powershell-framework') -Force; Import-Module (Join-Path -Path (Get-IcingaForWindowsRootPath) -ChildPath 'icinga-powershell-framework') -Global -Force; - - Write-IcingaConsoleNotice 'Installation of component "{0}" with version "{1}" was successful. Open a new PowerShell to apply the changes' -Objects $Name.ToLower(), $ManifestFile.ModuleVersion; } else { <# Handles installation of Icinga for Windows service