Skip to content

Commit

Permalink
Merge pull request #650 from Icinga:feature/adds_support_to_run_icing…
Browse files Browse the repository at this point in the history
…a_while_creating_no_new_instance

Feature: Adds support to not load a new PowerShell instance for command icinga

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, instead of create a new shell instance.

Does not support the argument `-DeveloperMode` in the same call, as Icinga for Window is dependent on these files for the current session.
  • Loading branch information
LordHepipud committed Jul 24, 2023
2 parents fbd286f + 7f07811 commit 64c3b46
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/100-General/10-Changelog.md
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions icinga-powershell-framework.psm1
Expand Up @@ -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');
Expand Down Expand Up @@ -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' `
Expand Down Expand Up @@ -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.';
Expand Down

0 comments on commit 64c3b46

Please sign in to comment.