I've written some code in powershell modules, and when my .ps1
file Imports them, it works with a fresh window; but if I stop the script, make some changes to the modules, and re-run the script...for some reason it runs just like it did before the changes were made; I can't test the changes to the script until I open a new window, and re-run the script.
I've tried adding things like Remove-Module <path-to-psm1> -Global
, but it still behaves the same; in Powershell 5 and Powershell 7; what else does one need to do to obtain a fresh copy of the module?
Truth be told however, I am nesting the imports, but for a good reason, since some of my modules are called as separate jobs, and so they need to be imported within the job; and additionally the *.psm1
's are also imported into another psm1 file that kind of maps their functions (function pointers more or less) within a HashMap.
So basically:
$dasMods = @("$PSScriptRoot\lib\mod1.psm1";
"$PSScriptRoot\lib\mod2.psm1";
"$PSScriptRoot\lib\mod3.psm1";
"$PSScriptRoot\lib\mod4.psm1";
"$PSScriptRoot\conf\label_function_log_mapper.psm1";)
$dasMods | % { Remove-Module $_ -Global }
$dasMods | % { Import-Module $_ -Global }
...
go.ps1
Remove-Module -Name .\lib\lib1.psm1 -Global
Import-Module -Name .\lib\lib1.psm1 -Global
Remove-Module -Name .\lib\lib2.psm1 -Global
Import-Module -Name .\lib\lib2.psm1 -Global
Remove-Module -Name .\lib\lib3.psm1 -Global
Import-Module -Name .\lib\lib3.psm1 -Global
...
\conf\label_function_log_mapper.psm1
function doSomething($fileObj, $jlist) {
Import-Module "$((Get-Item -Path "." -Verbose).FullName)\lib\lockAndLog.psm1"
lockDelogAndWrite "Did Something..." $fileObj
return @();
}
...
\lib\lib1.psm1
yes it's worse than that, there's a library for locking and writing to the log here that's imported too...
But for whatever reason I have to reopen a new powershell window, every time I make a change to \lib\libX.psm1
Is there a way around this? Where it can just re-run the script and also reload all the modules?