Import-Module ..\moduleFolder\

Use this forum to ask questions after your subscription maintenance expires or before you buy. Need information on licensing or pricing? Questions about a trial version? This is the right place for you. No scripting questions, please.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.
laned101
Posts: 28
Joined: Thu Aug 16, 2018 3:36 am
Answers: 1

Import-Module ..\moduleFolder\

Post by laned101 »

Firstly no idea why this keeps post here (Former and Future Customers - Questions) I have posted this in PowerShell Studio Application Support twice and it somehow ends up here again.

Anyway

Product: PowerShell Studio 2019 (64 Bit)
Build: v5.6.167
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.17763.0
PowerShell version: 5

I have a few questions relating to a deployment I am attempting to developed.

Firstly I am attempting to import custom modules within another custom module.

Here is the structure just to help paint the context;

Structure
Untitled-2.png
Untitled-2.png (18.3 KiB) Viewed 556 times
Module3.psm1 is attempting to import Module2.psm1 etc

Example
Import-Module ..\Module2 -Verbose
Import-Module ..\Module1 -Verbose

I get the following errors:

ERROR: Import-Module : The specified module '..\Module2' was not loaded because no valid module file was found in any module directory.
Module3.psm1 (16, 1): ERROR: At Line: 16 char: 1
ERROR: + Import-Module ..\Module2 -Verbose
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : ResourceUnavailable: (..\Module2:String) [Import-Module], FileNotFoundException
ERROR: + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
ERROR:


Ideally Id like to build the main scripts that import the modules into a single binary (exe), rather than have loads of modules that can be edited once deployed. But I will get to that once I work out how to actually import the modules.

Any help would be appreciated.

User avatar
Alexander Riedel
Posts: 7564
Joined: Tue May 29, 2007 4:43 pm
Answers: 2
Been upvoted: 5 times

Re: Import-Module ..\moduleFolder\

Post by Alexander Riedel »

You get redirected here because your subscription is expired.
Only active license subscriptions are eligible to post in the support forum.

But I will try to save you a lot of time. Modules don’t work that way.
They are not incorporated into a binary exe. That would defeat the very purpose of modules. Modules are about sharing code between scripts that use these modules. Think of it as a DLL.

What you want to do is dot source external code. Different mechanism and the packager can resolve those if you use a specific path (relative or absolute).
If you calculate the path at runtime that can obviously not be resolved at build time.
Alexander Riedel
SAPIEN Technologies, Inc.

laned101
Posts: 28
Joined: Thu Aug 16, 2018 3:36 am
Answers: 1

Re: Import-Module ..\moduleFolder\

Post by laned101 »

Thanks for the response, any idea why it wont let me import using ..\?

User avatar
Alexander Riedel
Posts: 7564
Joined: Tue May 29, 2007 4:43 pm
Answers: 2
Been upvoted: 5 times

Re: Import-Module ..\moduleFolder\

Post by Alexander Riedel »

Without the actual code, that is impossible to tell.
I'd suggest to post a boilerplate setup in the general PowerShell section.
Alexander Riedel
SAPIEN Technologies, Inc.

laned101
Posts: 28
Joined: Thu Aug 16, 2018 3:36 am
Answers: 1

Re: Import-Module ..\moduleFolder\

Post by laned101 »

Ive given you all the code that is relevant `Import-Module ..\Model3\Model3` along with the structure. This works perfectly fine within ISE and Powershell console from the relevant directory, it just doesn't work in PSS.

User avatar
Alexander Riedel
Posts: 7564
Joined: Tue May 29, 2007 4:43 pm
Answers: 2
Been upvoted: 5 times

Re: Import-Module ..\moduleFolder\

Post by Alexander Riedel »

This post here doesn't really contain any code, just a relative structure, which means nothing out of the context of your computer.
The "works perfectly fine in ISE and PowerShell console" statement is not helping, since it means nothing out of context.
PowerShell Studio uses the same "PowerShell" as the ISE or the console, so there is no difference.
The ISE and the console suffer from what is called runspace contamination. Unless you restart them every single time, and preferably without profile, any results are tainted by previously executed code.
If you imported a module once successfully, any subsequent import of the same module generally just works, since it is already loaded.

If PowerShell reports a missing module, it cannot find it. Since it is a PowerShell error message it has nothing to do with PowerShell Studio.
Looking at what you posted originally, "Import-Module ..\Module2 -Verbose", that would try to load a module from one folder level ABOVE the current folder.
I am not certain that this is intended.
Additionally, as far as I know, you normally would specify the specific module and not just a path, so
Import-module "../module2/module2.psm1"
would be what I would do.

But once again, please post a detailed question in the PowerShell section. Attach actual files someone can use to see what you are doing.
You need to also include how and where you execute your code. Your original post mentions packaging as an executable. It is not clear from your post if you already do that and execute the exe or just a script.
In what folder? Are the module folders relative to that or something else?
Details matter, so please be as specific as possible.
Alexander Riedel
SAPIEN Technologies, Inc.