Powershell module question

Ask your Windows 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.
Locked
User avatar
azl6j4s
Posts: 3
Joined: Thu Feb 11, 2016 1:26 pm

Powershell module question

Post by azl6j4s » Wed Sep 26, 2018 9:35 am

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.

Code: Select all

$FunctionsToExport = ( Get-ChildItem $PSScriptRoot\Functions\*.ps1 )
foreach ($function in $FunctionsToExport)
{
$function
. $function.FullName
}

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell module question

Post by jvierra » Wed Sep 26, 2018 9:42 am

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.

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell module question

Post by jvierra » Wed Sep 26, 2018 9:46 am

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
Joined: Thu Aug 18, 2011 4:56 am

Re: Powershell module question

Post by davidc » Wed Sep 26, 2018 9:52 am

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
Joined: Thu Feb 11, 2016 1:26 pm

Re: Powershell module question

Post by azl6j4s » Wed Sep 26, 2018 9:56 am

Thanks David. I will give it a try. Did not know of that feature. Thanks.

User avatar
azl6j4s
Posts: 3
Joined: Thu Feb 11, 2016 1:26 pm

Re: Powershell module question

Post by azl6j4s » Wed Sep 26, 2018 10:33 am

jvierra, I appreciated your comments as well. The link was helpful in getting my mindset around creating modules.

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell module question

Post by jvierra » Wed Sep 26, 2018 10:41 am

As David has pointed out, it is much easier and safer to allow PSS to build the module.

Locked