Shared function not working in Powershell Studio 2020?

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.
Post Reply
ServerbeheerRail
Posts: 5
Joined: Tue Dec 08, 2020 2:01 am

Shared function not working in Powershell Studio 2020?

Post by ServerbeheerRail »

Hello,

I created a project in Powershell Studio 2020 with two files, one script-file and one module-file.
In the module-files there are functions which I want to use in the whole project (more scripting-files has to be added).
In the script-file I use a function from the Module-file, now the function-name is grey which means that it's not recognized.
When I enable the setting Shared on the Module-file the function is recognized (it gets light blue).
When I run the script I get the error that the function is still not recognized.

What I want to achieve is to use the functions in the Module-file but not copy the Module-file to one of the default Module-paths.
Attachments
PS Studio - Setting Shared.jpg
PS Studio - Setting Shared.jpg (71.04 KiB) Viewed 1075 times
PS Studio - Error.jpg
PS Studio - Error.jpg (41.61 KiB) Viewed 1075 times
by jvierra » Tue Dec 29, 2020 5:55 am
Add file to project and set "Shared=True" on file properties page.

https://info.sapien.com/manuals/powersh ... rties.html

The "Product Manual" is the best place to start when learning how to use PowerShell Studio.
https://info.sapien.com/manuals/powersh ... index.html

The manual is linked on the "Help" tab of PSS.
Go to full post

jvierra
Posts: 14676
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

Re: Shared function not working in Powershell Studio 2020?

Post by jvierra »

You can explicitly load a module from the script folder by just specifying its path and module name.

In a project you can include the code in the project as an included shared file.

I recommend that shared functions should be in PS1 files and not part of modules. If you need to distributer modules then you will either need to add them to a module path \or import the module using the pathname of the module file.

ServerbeheerRail
Posts: 5
Joined: Tue Dec 08, 2020 2:01 am

Re: Shared function not working in Powershell Studio 2020?

Post by ServerbeheerRail »

Thanks for the quick reply.
I also tried the .ps1 and the option "Shared", that had the same result as I descripted.

I also tried the command . .\script.ps1 in the main script, that works.
But does this still work when I compile the script/project to an executable?
Last edited by ServerbeheerRail on Tue Dec 29, 2020 12:27 am, edited 1 time in total.

jvierra
Posts: 14676
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

Re: Shared function not working in Powershell Studio 2020?

Post by jvierra »

dot sourcing scripts should cause them to be included in the project. The script will only be inserted into the current scope. To insert globally load in "globals".

A script that is a set of functions will be included when shared. A module will not normally work as it may have dependencies that will not be correct when used as a PS1 function library.

ServerbeheerRail
Posts: 5
Joined: Tue Dec 08, 2020 2:01 am

Re: Shared function not working in Powershell Studio 2020?

Post by ServerbeheerRail »

Oke, I'm going to use the .ps1 instead of the .psm1.
Last question to check if I understand it correctly. In every script-file I add to the project I have to add . .\functions.ps1?

jvierra
Posts: 14676
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

Re: Shared function not working in Powershell Studio 2020?

Post by jvierra »

No. Just add the file as a shared file. It will be loaded globally to the program and combined into the final PS1 file.

ServerbeheerRail
Posts: 5
Joined: Tue Dec 08, 2020 2:01 am

Re: Shared function not working in Powershell Studio 2020?

Post by ServerbeheerRail »

I searched the Sapien website and looked into the settings in Powershell Studio, but I can't find where I can add the file as a shared file.
Maybe a newby question, but where can I do this?

Some more background.
I have a few scripts that have to use the same functions, see the attached screenshot.
At the end I want to create seperate .exe from the Get-OS-Script and Get-Services-Script files. The two scripts do not have a relation to each other.
When I enter the . .\functions.ps1 in the script and enable the package-setting "Resolve and include extenral scripts" it works. But then I have to enter the functions.ps1 in every script and not globally.

Thanks for your help so far.
Attachments
PS Studio - Project.jpg
PS Studio - Project.jpg (6.73 KiB) Viewed 1036 times
PS Studio - setting.jpg
PS Studio - setting.jpg (53.05 KiB) Viewed 1036 times

jvierra
Posts: 14676
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

Re: Shared function not working in Powershell Studio 2020?

Post by jvierra »

Add file to project and set "Shared=True" on file properties page.

https://info.sapien.com/manuals/powersh ... rties.html

The "Product Manual" is the best place to start when learning how to use PowerShell Studio.
https://info.sapien.com/manuals/powersh ... index.html

The manual is linked on the "Help" tab of PSS.

Post Reply