Page 1 of 1

Powershell module question

Posted: Wed Sep 26, 2018 9:35 am
by azl6j4s
Newbie question Powershell Studio 2018

I would like to keep modules clean, meaning putting functions in its own folder within the PS2018 project. When I try to add them into the manifest (FunctionsToExport) section, they disappear after building the module. I use this code in the .psm1 file figuring it would load them. Am I doing something incorrect or in the wrong order?

Any help would be appreciated.
  1. $FunctionsToExport = ( Get-ChildItem $PSScriptRoot\Functions\*.ps1 )
  2.  
  3. foreach ($function in $FunctionsToExport)
  4.  
  5. {
  6.  
  7.         $function
  8.  
  9.     . $function.FullName
  10.  
  11. }

Re: Powershell module question

Posted: Wed Sep 26, 2018 9:42 am
by jvierra
Not sure what you mean by "disappear".

Why do you think you want functions in separate files? That is not normal for module design.

A PS1 file is NOT a function. It is a script and must be referenced as a script.

Re: Powershell module question

Posted: Wed Sep 26, 2018 9:46 am
by jvierra
Here are some issues that you will run into and need to be aware of:

https://mikefrobbins.com/2018/09/21/pow ... hilosophy/

Re: Powershell module question

Posted: Wed Sep 26, 2018 9:52 am
by davidc
Disable the Auto Export Functions feature in the project settings. Refer to the following article for more details:

https://www.sapien.com/blog/2018/08/14/ ... e-support/

You can keep all your functions individual files and have PowerShell Studio merge them into a single psm script when the module is Built. This way you don't have to rely on a custom script within the manifest.
Set each ps1 file's Build and Shared properties:

Build = Include
Shared = True

Then PowerShell Studio will handle the rest. If you keep the Auto Export Functions option enabled, you need not touch the manifest.
The added benefit of keeping the manifest static is that it will load faster.

Re: Powershell module question

Posted: Wed Sep 26, 2018 9:56 am
by azl6j4s
Thanks David. I will give it a try. Did not know of that feature. Thanks.

Re: Powershell module question

Posted: Wed Sep 26, 2018 10:33 am
by azl6j4s
jvierra, I appreciated your comments as well. The link was helpful in getting my mindset around creating modules.

Re: Powershell module question

Posted: Wed Sep 26, 2018 10:41 am
by jvierra
As David has pointed out, it is much easier and safer to allow PSS to build the module.