SAPIEN PowerShellStudio does not find functions in included files

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 9 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
therider
Posts: 6
Last visit: Tue Jul 03, 2018 9:43 am

SAPIEN PowerShellStudio does not find functions in included files

Post by therider »

I have two basic PowerShell script files as follows

script1.ps1
  1. # script1.ps1
  2. function print
  3. {
  4.   Write-Output "hello"
  5. }
script2.ps1
  1. # script2.ps1
  2. . "script1.ps1"
  3. print # This call works but it's greyed out
In SAPIEN PowerShell studio, I can run script2.ps1 and it will work fine, but the function "print" is greyed out, which is both visually distracting since it's incorrectly stating the function is "unknown" and makes it so I can't right click and go straight to the method.

Can I configure SAPIEN in a way such that it can find functions from included files? Perhaps there's another way to include the file?

Thanks.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: SAPIEN PowerShellStudio does not find functions in included files

Post by mxtrinidad »

Sorry! But you can't do that!

To load the function, it need to be part of the script or part of the PowerShell Profile, or be loaded as part of a module.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SAPIEN PowerShellStudio does not find functions in included files

Post by jvierra »

Or … in a project place the functions in the "globals.ps1" file.

Or … deploy the scripts as PS1 files and "dot source" them in the main script.

As you have found, the external scripts are not subject to intellisence. Project global functions are.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: SAPIEN PowerShellStudio does not find functions in included files

Post by davidc »

You are probably calling print.exe and not your function.

Use this instead:
  1. . ".\script1.ps1"
David
SAPIEN Technologies, Inc.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: SAPIEN PowerShellStudio does not find functions in included files

Post by mxtrinidad »

At the same time, for PowerShell Best Practice, you should follow PowerShell naming schema Verb-Noun format. Avoid use of possible reserved words or existing application names.

You could use "Get-Print" instead of "Print" as a function name.

https://blogs.technet.microsoft.com/pst ... practices/
This topic is 5 years and 9 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