Load Multiple Modules (.psm1) using a single psd1 file

rated by 0 users
This post has 5 Replies | 1 Follower

Top 500 Contributor
Posts 7
Angshuman Posted: 04-18-2012 8:55 AM

Hi,

I want to  organize functions into multiple .psm1 files and have them loaded by a single Module Manifest file (.psd1)So, the .psd1 and multiple .psm1 files would sit under the same "module_name/" directory. Only the .psd1 file would need to have the same name as the module.

 

I think it should be possible. Can anyone help me out please ?

 

Top 10 Contributor
Posts 628
Microsoft MVP
Top Contributor

A module is composed of scripts or dlls, not of additional modules.

There can be additional dependencies (other required modules), but they would still stay separate modules.

What you can do is use a generic psm1 loader file like this:

gci $psscriptroot\*.ps1 | % { . $_.FullName }

Then, in your psd1 file, specify the psm1 file as ModuleToProcess.

This way, you can paste whatever functions you want into your module folder. Just make sure you save each function as ps1 file. Then, when you import the module, the dynamic loader will load whatever ps1 file it finds.

Top 500 Contributor
Posts 7

But, is it possible to call any function directly inside the loaded .ps1 files [as you say above] directly like Modules ? I am asking this because I am struggling to call a parameterised function inside a ps1 file using dot-sourcing from command line-

 

function f([string]$a, [string]$b)
{
  Write-Host "a:", $a, " b:", $b
}
Save the above as Test.ps1 and load from the commad prompt as .\Test.ps1 & then call the function as f "a" "b"
I get following error - 
The term 'f' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:2
+ f <<<<  "a" "b"
    + CategoryInfo          : ObjectNotFound: (f:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

 

Top 10 Contributor
Posts 628
Microsoft MVP
Top Contributor

you need to really dot-source the script :-)

. .\test.ps1

Note that the first dot is the dot-sourcing, and the second one is part of the file path, representing the current folder.

 

Top 500 Contributor
Posts 7

Sorry, I think I am missing something. I tried what you suggested, but I get the following error. I ran using Powershell x86 command prompt shell as well as ISE. I am running from the directory where the file is saved i.e. same level

 

The term '..\Test.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the

 spelling of the name, or if a path was included, verify that the path is correct and try again.

At line:1 char:12

+ ..\Test.ps1 <<<<

    + CategoryInfo          : ObjectNotFound: (..\Test.ps1:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

Top 500 Contributor
Posts 7

I corrected my issues. Thanks, you were right. It was a typo. It should be . .\test.ps1 [Note the space between dots] & this article from Snover clears all doubts regarding dot-sourcing...http://blogs.msdn.com/b/powershell/archive/2007/06/19/get-scriptdirectory.aspx - Brilliant !!!

Page 1 of 1 (6 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.