Module with ability to open functions and cmdlets

Hi,
I’m building a new module for some multiple specific functions and it works fine. However, I’m trying to figure out a way to use those functions as scripts with parameters as well.

Basically, all my functions are stored in .\functions\filename.ps1 and they all start with “Function XXX {” as it normally should. My psm1 file cycles through each file and loads them into memory - works fine.
I’m looking for a way to be able to also execute the files individually so that anyone within the .\Functions folder can execute the “.\Filename.ps1 -Param1 XXXX -Param2 XXX”.

I tried to remove the “Function XXXX {” from the ps1 files and inject the start and end lines when we load the module but it doesn’t work so well and even though it works, it doesn’t actually load as a valid Module.

Surely there is another way I should be doing this? Trying to make it easy for us to use as a module or individually in case anyone wants to skip loading the entire module or even just for quicker development without reloading the module each time.

Any help is appreciated. Thanks.

slicster,
Welcome to the forum. :wave:t4:

When your module is properly installed it gets loaded automatically when a function from it is called. There’s no need to make it available with an additional way.

Hi slicster and welcome.
I agree with Olaf that when the module is deployed correctly it will automatically load all functions and there’s no need to call them separately.

However, if you’re still developing it and wish to call a function separately there are ways to go about this too.
If I understand you correctly each function has it’s own file .ps1 under .\functions\ and if so you can dot source the file which will load the function into memory.
Then you call the function directly with the full path and necessary parameters.

PS C:\> . C:\Dev\Module\Functions\Get-MyInfo.ps1
Get-MyInfo -Parameter1 'String' -Parameter2 2

If you instead have all functions in the same file ‘FileName.ps1’ you would need to source or dot source the file first which in many ways is what the module does when it’s initialized anyway.
Once you’ve sourced the file you can call any function in there as you would any other cmdlet.

PS C:\> . C:\Dev\Module\Functions\FileName.ps1
Get-MyInfo -Parameter1 'String' -Parameter2 2

This should only be used during testing and not when the finished module is deployed to end-users.
And I’d argue that even during testing it would be better to set up a workflow where you for each test copy out the module and do a full Import-Module -Force for each run of the tests.

1 Like

Thanks for the replies. My module works perfectly but my issue is that we have a lot of functions within and there is currently lots of development. So the issue really is reloading the module -force each time to test something. It would be so practical if, by default, there was a way to have it without “Function XXX {” and that the .psm1 loading would add or assume to create them as function.

I know, I know I can juste remove them myself manually during tests but I often jump through multiple scripts so it’s a bit of a pain. Just making small mods on multiple files, easily test and when testing is good, just add a commit and push.

One way that seemed promising was to use “out-file” and “add-content” to recreate the files with “Function XXX {” and “}” at the end but the module would then take longer to load and for some reason, some functions no longer worked. Didn’t really dig into it more as I thought maybe there was a way that already existed instead of reinventing the wheel.

If it’s not possible, of course it’s livable but it really would be cool.