Skip to content

Commit 64c3b46

Browse files
authored
Merge pull request #650 from Icinga:feature/adds_support_to_run_icinga_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.
2 parents fbd286f + 7f07811 commit 64c3b46

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
2525
### Enhancements
2626

2727
* [#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
28+
* [#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
2829
* [#619](https://github.com/Icinga/icinga-powershell-framework/pull/619) Adds feature to securely read enum provider values with new function `Get-IcingaProviderEnumData`
2930
* [#623](https://github.com/Icinga/icinga-powershell-framework/issues/623) Adds support to provide the Icinga service user written as `user@domain`
3031
* [#633](https://github.com/Icinga/icinga-powershell-framework/pull/633) Adds support for Icinga 2.14.0 native Icinga for Windows API communication

icinga-powershell-framework.psm1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ function Import-IcingaForWindowsModulesInSession()
8484
}
8585
}
8686

87+
function Import-IcingaForWindowsModules()
88+
{
89+
[array]$IcingaForWindowsModules = Get-ChildItem -Path (Get-IcingaForWindowsRootPath);
90+
91+
foreach ($module in $IcingaForWindowsModules) {
92+
if ($module.Name -Like 'icinga-powershell-*') {
93+
try {
94+
Import-Module $module.Name -Force -ErrorAction Stop;
95+
Import-Module $module.Name -Force -Global -ErrorAction Stop;
96+
} catch {
97+
Write-Host ([string]::Format('Failed to import Icinga for Windows module "{0}": {1}', $module.Name, $_.Exception.Message));
98+
}
99+
}
100+
}
101+
102+
Import-Module 'icinga-powershell-framework' -Force;
103+
Import-Module 'icinga-powershell-framework' -Force -Global;
104+
}
105+
87106
function Get-IcingaFrameworkCodeCacheFile()
88107
{
89108
return (Join-Path -Path (Get-IcingaCacheDir) -ChildPath 'framework_cache.psm1');
@@ -249,9 +268,15 @@ function Invoke-IcingaCommand()
249268
[switch]$NoSSLValidation = $FALSE,
250269
[switch]$RebuildCache = $FALSE,
251270
[switch]$DeveloperMode = $FALSE,
271+
[switch]$NoNewInstance = $FALSE,
252272
[array]$ArgumentList = @()
253273
);
254274

275+
If ($DeveloperMode -And $NoNewInstance) {
276+
Write-Host 'DeveloperMode is not supported while using NoNewInstance argument.' -ForegroundColor red;
277+
return;
278+
}
279+
255280
Import-LocalizedData `
256281
-BaseDirectory (Get-IcingaFrameworkRootPath) `
257282
-FileName 'icinga-powershell-framework.psd1' `
@@ -285,6 +310,14 @@ function Invoke-IcingaCommand()
285310
Write-IcingaFrameworkCodeCache -DeveloperMode:$DeveloperMode;
286311
}
287312

313+
# Try to re-import everything within the same instance
314+
if ($NoNewInstance) {
315+
Import-IcingaForWindowsModules;
316+
Use-Icinga;
317+
318+
return;
319+
}
320+
288321
if ($null -ne $psISE) {
289322
Use-Icinga;
290323
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.';

0 commit comments

Comments
 (0)