Merge pull request #601 from Icinga:fix/unintended_ps_location_change

Fix: Unintended PS path change

Fixes installation and uninstallation commands changing PowerShell location even when not necessary
This commit is contained in:
Lord Hepipud 2022-12-19 14:14:13 +01:00 committed by GitHub
commit d80ea57bf8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 3 deletions

View file

@ -13,6 +13,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
### Bugfixes ### Bugfixes
* [#578](https://github.com/Icinga/icinga-powershell-framework/issues/578) Fixes installation and uninstallation commands changing PowerShell location even when not necessary
* [#582](https://github.com/Icinga/icinga-powershell-framework/issues/582) Fixes background service registration caused by migration task for v1.10.0 being executed even when no services were defined before * [#582](https://github.com/Icinga/icinga-powershell-framework/issues/582) Fixes background service registration caused by migration task for v1.10.0 being executed even when no services were defined before
* [#588](https://github.com/Icinga/icinga-powershell-framework/issues/588) Fixes threshold values causing an error because of too aggressive regex expression * [#588](https://github.com/Icinga/icinga-powershell-framework/issues/588) Fixes threshold values causing an error because of too aggressive regex expression
* [#599](https://github.com/Icinga/icinga-powershell-plugins/issues/599) Fixes plugin argument parser to proceed with real argument names and possible provided aliases * [#599](https://github.com/Icinga/icinga-powershell-plugins/issues/599) Fixes plugin argument parser to proceed with real argument names and possible provided aliases

View file

@ -5,6 +5,8 @@ function Write-IcingaForWindowsComponentCompilationFile()
[string]$CompiledFilePath = '' [string]$CompiledFilePath = ''
); );
# Store our current shell location
[string]$OldLocation = Get-Location;
# Get the current location and leave this folder # Get the current location and leave this folder
Set-Location -Path $ScriptRootPath; Set-Location -Path $ScriptRootPath;
Set-Location -Path '..'; Set-Location -Path '..';
@ -57,4 +59,7 @@ function Write-IcingaForWindowsComponentCompilationFile()
Import-Module -Name $ModulePath -Force; Import-Module -Name $ModulePath -Force;
Import-Module -Name $ModulePath -Force -Global; Import-Module -Name $ModulePath -Force -Global;
# Set our location back to the previous folder
Set-Location -Path $OldLocation;
} }

View file

@ -45,7 +45,7 @@ function Uninstall-IcingaForWindows()
} }
} }
Set-Location -Path (Get-IcingaForWindowsRootPath); Set-IcingaPSLocation;
Write-IcingaConsoleNotice 'Uninstalling Icinga for Windows from this host'; Write-IcingaConsoleNotice 'Uninstalling Icinga for Windows from this host';
Write-IcingaConsoleNotice 'Uninstalling Icinga Security configuration if applied'; Write-IcingaConsoleNotice 'Uninstalling Icinga Security configuration if applied';

View file

@ -28,7 +28,7 @@ function Uninstall-IcingaComponent()
} }
# Set our current location to the PowerShell modules folder, to prevent possible folder lock during uninstallation # Set our current location to the PowerShell modules folder, to prevent possible folder lock during uninstallation
Set-Location (Get-IcingaForWindowsRootPath); Set-IcingaPSLocation -Path $UninstallPath;
Write-IcingaConsoleNotice -Message 'Uninstalling Icinga for Windows component "{0}" from "{1}"' -Objects $UninstallComponent, $UninstallPath; Write-IcingaConsoleNotice -Message 'Uninstalling Icinga for Windows component "{0}" from "{1}"' -Objects $UninstallComponent, $UninstallPath;
if (Remove-ItemSecure -Path $UninstallPath -Recurse -Force) { if (Remove-ItemSecure -Path $UninstallPath -Recurse -Force) {
@ -36,7 +36,7 @@ function Uninstall-IcingaComponent()
if ($UninstallComponent -ne 'icinga-powershell-framework') { if ($UninstallComponent -ne 'icinga-powershell-framework') {
Remove-Module $UninstallComponent -Force -ErrorAction SilentlyContinue; Remove-Module $UninstallComponent -Force -ErrorAction SilentlyContinue;
# In case we are not removing the framework itself, set the location to the Icinga for Windows Folder # In case we are not removing the framework itself, set the location to the Icinga for Windows Folder
Set-Location (Get-IcingaFrameworkRootPath); Set-IcingaPSLocation -Path $UninstallPath;
} }
return $TRUE; return $TRUE;
} else { } else {

View file

@ -0,0 +1,21 @@
function Set-IcingaPSLocation()
{
param (
[string]$Path = (Get-Location)
);
if ([string]::IsNullOrEmpty($Path)) {
return;
}
if ((Test-Path $Path) -eq $FALSE) {
return;
}
[string]$IfWRootPath = Get-IcingaForWindowsRootPath;
[string]$CurrentPath = Get-Location;
if ($CurrentPath -Like ([string]::Format('{0}*', $Path))) {
Set-Location -Path $IfWRootPath;
}
}