Adds feature to not load a new PowerShell instance for command icinga

This commit is contained in:
LordHepipud 2023-07-24 22:23:28 +02:00
parent fbd286f369
commit 7f07811a63
2 changed files with 34 additions and 0 deletions

View file

@ -25,6 +25,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
### Enhancements
* [#544](https://github.com/Icinga/icinga-powershell-framework/issues/544) Adds support to configure the Icinga Director JSON string for registering hosts via self-service API
* [#573](https://github.com/Icinga/icinga-powershell-framework/issues/573) Adds support to run command `icinga` with new argument `-NoNewInstance`, to use `-RebuildCache` as example to update the current PowerShell instance with all applied changes
* [#619](https://github.com/Icinga/icinga-powershell-framework/pull/619) Adds feature to securely read enum provider values with new function `Get-IcingaProviderEnumData`
* [#623](https://github.com/Icinga/icinga-powershell-framework/issues/623) Adds support to provide the Icinga service user written as `user@domain`
* [#633](https://github.com/Icinga/icinga-powershell-framework/pull/633) Adds support for Icinga 2.14.0 native Icinga for Windows API communication

View file

@ -84,6 +84,25 @@ function Import-IcingaForWindowsModulesInSession()
}
}
function Import-IcingaForWindowsModules()
{
[array]$IcingaForWindowsModules = Get-ChildItem -Path (Get-IcingaForWindowsRootPath);
foreach ($module in $IcingaForWindowsModules) {
if ($module.Name -Like 'icinga-powershell-*') {
try {
Import-Module $module.Name -Force -ErrorAction Stop;
Import-Module $module.Name -Force -Global -ErrorAction Stop;
} catch {
Write-Host ([string]::Format('Failed to import Icinga for Windows module "{0}": {1}', $module.Name, $_.Exception.Message));
}
}
}
Import-Module 'icinga-powershell-framework' -Force;
Import-Module 'icinga-powershell-framework' -Force -Global;
}
function Get-IcingaFrameworkCodeCacheFile()
{
return (Join-Path -Path (Get-IcingaCacheDir) -ChildPath 'framework_cache.psm1');
@ -249,9 +268,15 @@ function Invoke-IcingaCommand()
[switch]$NoSSLValidation = $FALSE,
[switch]$RebuildCache = $FALSE,
[switch]$DeveloperMode = $FALSE,
[switch]$NoNewInstance = $FALSE,
[array]$ArgumentList = @()
);
If ($DeveloperMode -And $NoNewInstance) {
Write-Host 'DeveloperMode is not supported while using NoNewInstance argument.' -ForegroundColor red;
return;
}
Import-LocalizedData `
-BaseDirectory (Get-IcingaFrameworkRootPath) `
-FileName 'icinga-powershell-framework.psd1' `
@ -285,6 +310,14 @@ function Invoke-IcingaCommand()
Write-IcingaFrameworkCodeCache -DeveloperMode:$DeveloperMode;
}
# Try to re-import everything within the same instance
if ($NoNewInstance) {
Import-IcingaForWindowsModules;
Use-Icinga;
return;
}
if ($null -ne $psISE) {
Use-Icinga;
Write-IcingaConsoleError -Message 'Icinga for Windows was loaded, but the Icinga Management Console is not available within the PowerShell ISE context. Please start a regular PowerShell to use it.';