Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I reload modules from one open work environment to affect another working environment
    text
    copied!<p>I have my PowerShell project broken into modules. But because they are modules I have to reload them every time I change them. So I wrote a loop that has a FileSystemWatcher and if one of the .psm1 file changes it will either reload or import that module.</p> <p>The problem is that the above loop isn't going to let me run other scripts in its working environment, so a new environment will not have the same modules loaded/reloaded for it. I need to keep these modules out of the primary default PowerShell modules folder(s). Is there a way to run the script that reloads the modules when they change in the same environment or affect a certain environment?</p> <hr> <p><strong>UPDATE</strong></p> <p>So I run the following Module-Loader.ps1 script. The code block associated with the 'FileChanged' event does fire when I 'save' a *.psm1 file after having been modified. However two issues occure: 1) it fires twice when I save</p> <p>2a) If the module is not loaded, it will run Import-Module $PWD\ModuleName, but it won't have actually loaded at least in the environment (if I run the same code in the environment it will load)</p> <p>2b) if it is loaded, and it tries to remove the module, it will error that none exists.</p> <pre><code># create a FileSystemWatcher on the currect directory $filter = '*.psm1' $folder = $PWD $watcher = New-object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false; EnableRaisingEvents = $true; NotifyFilter = [IO.NotifyFilters]'LastWrite'} Register-ObjectEvent $watcher Changed -SourceIdentifier FileChanged -Action { $name = $Event.SourceEventArgs.Name $filename = $name.Remove($name.IndexOf('.'), 5) $loadedModule = Get-Module | ? { $_.Name -eq $filename } write-host $filename if ($loadedModule) { write-host "Reloading Module $folder\$($filename)" Reload-Module $folder\$filename } else { write-host "Importing Module $folder\$($filename)" Import-Module $folder\$filename } } </code></pre> <p>I am of the opinion that though this is being ran in a session, the code block in the event is not associated with this specific environment.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload