Powershell module question

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
azl6j4s
Posts: 3
Last visit: Fri Jan 04, 2019 12:29 pm

Powershell module question

Post 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. foreach ($function in $FunctionsToExport)
  3. {
  4.         $function
  5.     . $function.FullName
  6. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell module question

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell module question

Post 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/
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Powershell module question

Post 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.
David
SAPIEN Technologies, Inc.
User avatar
azl6j4s
Posts: 3
Last visit: Fri Jan 04, 2019 12:29 pm

Re: Powershell module question

Post by azl6j4s »

Thanks David. I will give it a try. Did not know of that feature. Thanks.
User avatar
azl6j4s
Posts: 3
Last visit: Fri Jan 04, 2019 12:29 pm

Re: Powershell module question

Post by azl6j4s »

jvierra, I appreciated your comments as well. The link was helpful in getting my mindset around creating modules.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell module question

Post by jvierra »

As David has pointed out, it is much easier and safer to allow PSS to build the module.
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked