0

I have some powershell modules which contain classes I need to instantiate from various powershell scripts. To access the classes from the powershell scripts I am have statements like:

  using module "..\..\Library\Mymodule.psm1"

But I dont know ahead of time where in the folder hierarchy my library will be relative to the script.

We dont want to use the standard powershell modules folders because these classes are under source control and deploying to user's folders would be nightmarish.

This is an extremely hard topic to google because, well "using" is used everywhere!

Tried provide multiple possible locations for the same file but of course we get an error for the alternate locations that do not exist:

The specified module 'C:...Mymodule.psm1' was not loaded because no valid module file was found in any module directory. At line:0 char:0

using module "..\..\Library\Mymodule.psm1"
using module "..\Library\Mymodule.psm1"
using module ".\Library\Mymodule.psm1"

But I'd rather run a function to first determine the correct module path, and then use something like this

using module "$foundModulePath"

Is there any way to dynamically set the path of a module and then "using" it?

Wes Baker
  • 1
  • 1
  • I created a profile script and add my module path to the `$env:PSModulePath `. After that I can import the modules. Maybe that helps for you too..? – Dan Stef Sep 26 '19 at 06:41
  • I did not know what a profile script was, so after a bit of research this does seem an option. I'll have a go at this and lftimie's answer as well, and report back – Wes Baker Sep 26 '19 at 23:24

1 Answers1

0

I would version my modules in a separate repository and write a function the clones that repository inside the workspace at runtime.

Using tags in that repo - would allow me to control the version of the modules I am loading for each script. This will make the development of the modules possible, even when a script still has to use the old versions and I don't want to "invest" time in re-writing it , just because I've changed a dependency.

idea no 2. Gradle is very good in handling messy dependencies lists in big projects.
You can build a gradle script that prepares the workspace and then, from inside of it, execute your powershell script. See Link here

TudorIftimie
  • 1,050
  • 11
  • 21
  • I really like the sounds of this suggestion, especially given the ability to control version dependencies. As a newbie It will take me some time to try and implement but I will report back here with some feedback soon. – Wes Baker Sep 26 '19 at 23:27