Export functions when building PSModule

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

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 10 months and 2 weeks 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.
User avatar
rpalmieri
Posts: 5
Last visit: Thu Feb 22, 2024 1:12 am

Export functions when building PSModule

Post by rpalmieri »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build:
Operating system: Windows 11
PowerShell version(s): PS 5.11

*** Please add details and screenshots as needed below. ***

DO NOT POST LICENSES, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM

Hi,

I have an issue when building a PSModule.
I have multiple functions in my PSModule, but want to export only a few.
So I have added at the end of my .psm1 file this line:

Code: Select all

Export-ModuleMember -Function 'Start-Function1, 'Get-Function1'
I have set the properties of the MyPSModule.psm1 file -> Export Functions to False

When building my MyPSModule.psm1 file gets build with this line:

Code: Select all

FunctionsToExport	   = @()
This seems to break the Module, and I cannot use it in Powershell
When I edit this line to:

Code: Select all

FunctionsToExport	   = @('Start-Function1, 'Get-Function1')
It works again, but this is a manual action I have to do with every build change.

So I am doing something wrong, but do not know what.

Thanks in advance,

Ramon
User avatar
brittneyr
Site Admin
Posts: 1654
Last visit: Wed Mar 27, 2024 1:54 pm
Answers: 39
Been upvoted: 30 times

Re: Export functions when building PSModule

Post by brittneyr »

What build of PowerShell Studio are you using? This information can be found under the About button in the top-right corner.

Export Functions automatically updates FunctionsToExport property for you when set to true.

In order to use the functions in your module when loaded into a script, the functions needs to be listed under FunctionsToExport.
Brittney
SAPIEN Technologies, Inc.
User avatar
rpalmieri
Posts: 5
Last visit: Thu Feb 22, 2024 1:12 am

Re: Export functions when building PSModule

Post by rpalmieri »

Hi Brittney,

Thanks for your reply
I use PS Studio 2023 V5.8.211

The issue that I have (had) is opposite as you are explaining.
I have one .psm1 file which contains public and private functions.
In order te prevent that the private functions will be shown to the end user, I have set the Export Functions setting to False.
I then added the code line

Code: Select all

Export-ModuleMember -Function 'Start-Function1, 'Get-Function1'
I also editted the .psd1 file to reflect this:

Code: Select all

FunctionsToExport	   = @('Start-Function1, 'Get-Function1')
And that works, but when I build the project PSStudio changes this line again to

Code: Select all

FunctionsToExport	   = @()
which breaks the working of the module just build.
I feel that PS Studio should leave that entry alone.

I found a workaround, or maybe it is even a better way of working, by adding .ps1 files to the project seperating public and private functions.
The public functions file will have the Export Functions setting to True and for the private functions to False

This works fine and I feel it is a better way of working, but I also feel that the behavior descripted initially is incorrect
User avatar
brittneyr
Site Admin
Posts: 1654
Last visit: Wed Mar 27, 2024 1:54 pm
Answers: 39
Been upvoted: 30 times

Re: Export functions when building PSModule

Post by brittneyr »

There are two locations to set to false in the properties pane when exporting functions: the script file and the project file.

For a script, setting Export Functions to false will prevent the export of functions from that file.

For a project file, setting Auto Export Functions to false will prevent the project from looking at any projects file export setting.

Please verify that Auto Export Functions is turned off on in your project settings:
SPS_PropertiesAutoExportFunctions.png
SPS_PropertiesAutoExportFunctions.png (20.12 KiB) Viewed 1684 times
Brittney
SAPIEN Technologies, Inc.
This topic is 10 months and 2 weeks 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.