From b2f8d33228544eeaeb9d373af709c9c873be7671 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Thu, 18 Jul 2019 17:52:21 +0200 Subject: [PATCH] Added improved import handler --- core/init.ps1 | 2 ++ icinga-module-windows.psd1 | 2 +- icinga-module-windows.psm1 | 68 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/core/init.ps1 b/core/init.ps1 index e97ce2e..3b604ef 100644 --- a/core/init.ps1 +++ b/core/init.ps1 @@ -7,6 +7,8 @@ param ( # Create an internal 'namespace' for our environment Set-Variable -Name Icinga2 -Option Constant -Value @{ Function = @( + 'Import-IcingaLib', + 'Import-IcingaDirectoryModules', 'Get-Icinga-Lib', 'Get-Icinga-Object', 'Get-Icinga-Service', diff --git a/icinga-module-windows.psd1 b/icinga-module-windows.psd1 index c3b2ac9..0a06bee 100644 --- a/icinga-module-windows.psd1 +++ b/icinga-module-windows.psd1 @@ -25,7 +25,7 @@ Description = 'Icinga 2 Windows Agent Module, which allows to entirely monitor t PowerShellVersion = '3.0' # Aus diesem Modul zu exportierende Funktionen. Um optimale Leistung zu erzielen, verwenden Sie keine Platzhalter und löschen den Eintrag nicht. Verwenden Sie ein leeres Array, wenn keine zu exportierenden Funktionen vorhanden sind. -FunctionsToExport = @( 'Start-Icinga-Checker', 'Stop-Icinga-Checker', 'Get-Icinga-Lib', 'Get-Icinga-Object', 'Get-Icinga-Service', 'Start-Icinga-Service', 'Stop-Icinga-Service', 'Restart-Icinga-Service', 'Install-Icinga-Service', 'Uninstall-Icinga-Service', 'Get-Icinga-Setup', 'Install-Icinga', 'Start-Icinga-Daemon', 'Stop-Icinga-Daemon', 'Icinga-Client', 'Get-Icinga-Command', 'New-Icinga-Monitoring', 'Get-Icinga-Counter', 'Get-Icinga-Config', 'Set-Icinga-Config', 'Remove-Icinga-Config', 'New-Icinga-Config' ) +FunctionsToExport = @( 'Import-IcingaLib', 'Import-IcingaDirectoryModules', 'Start-Icinga-Checker', 'Stop-Icinga-Checker', 'Get-Icinga-Lib', 'Get-Icinga-Object', 'Get-Icinga-Service', 'Start-Icinga-Service', 'Stop-Icinga-Service', 'Restart-Icinga-Service', 'Install-Icinga-Service', 'Uninstall-Icinga-Service', 'Get-Icinga-Setup', 'Install-Icinga', 'Start-Icinga-Daemon', 'Stop-Icinga-Daemon', 'Icinga-Client', 'Get-Icinga-Command', 'New-Icinga-Monitoring', 'Get-Icinga-Counter', 'Get-Icinga-Config', 'Set-Icinga-Config', 'Remove-Icinga-Config', 'New-Icinga-Config' ) # Aus diesem Modul zu exportierende Cmdlets. Um optimale Leistung zu erzielen, verwenden Sie keine Plat'zhalter und löschen den Eintrag nicht. Verwenden Sie ein leeres Array, wenn keine zu exportierenden Cmdlets vorhanden sind. CmdletsToExport = @() diff --git a/icinga-module-windows.psm1 b/icinga-module-windows.psm1 index 3d84e5e..e39a4c4 100644 --- a/icinga-module-windows.psm1 +++ b/icinga-module-windows.psm1 @@ -9,6 +9,74 @@ #> +function Import-IcingaLib() +{ + param( + [Parameter( + Position=0, + Mandatory=$true, + ValueFromPipeline=$true, + ValueFromPipelineByPropertyName=$true) + ] + [String]$Lib, + # The Force Reload will remove the module in case it's loaded and reload it to track + # possible development changes without having to create new PowerShell environments + [Switch]$ForceReload + ); + + [string]$directory = Join-Path -Path $PSScriptRoot -ChildPath 'lib'; + [string]$module = Join-Path -Path $directory -ChildPath $Lib; + $module = $module.Replace('.psm1', ''); # Cut possible .psm1 ending + [string]$moduleName = $module.Split('\')[-1]; # Get the last element + + if ($ForceReload) { + $ListOfLoadedModules = Get-Module | Select-Object Name; + if ($ListOfLoadedModules -Like "*$moduleName*") { + Remove-Module -Name $moduleName; + } + } + + Import-Module ([string]::Format('{0}.psm1', $module)) -Global; +} + +function Import-IcingaDirectoryModules() +{ + param( + [Parameter( + Position=0, + Mandatory=$true, + ValueFromPipeline=$true, + ValueFromPipelineByPropertyName=$true) + ] + [String]$LibDirectory, + # The Force Reload will remove the module in case it's loaded and reload it to track + # possible development changes without having to create new PowerShell environments + [Switch]$ForceReload + ); + + $LibDirectory = $LibDirectory.Replace('.psm1', ''); + + if (-Not (Test-Path $LibDirectory)) { + Write-Output ([string]::Format('Include directory not found: {0}', $LibDirectory)); + return; + } + + Get-ChildItem -Path $LibDirectory -Recurse -Filter *.psm1 | + ForEach-Object { + [string]$modulePath = $_.FullName; + [string]$moduleName = $_.Name.Replace('.psm1', ''); + + if ($ForceReload) { + $ListOfLoadedModules = Get-Module | Select-Object Name; + if ($ListOfLoadedModules -like "*$moduleName*") { + Remove-Module -Name $moduleName + } + } + + Import-Module ([string]::Format('{0}', $modulePath)) -Global; + } +} + function Install-Icinga() { [string]$command = Get-Icinga-Command('setup');