SAPIEN PowerShellStudio does not find functions in included files

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
therider
Posts: 6
Joined: Mon May 21, 2018 3:48 pm

SAPIEN PowerShellStudio does not find functions in included files

Post by therider » Wed Jun 20, 2018 9:46 am

I have two basic PowerShell script files as follows

script1.ps1

Code: Select all

# script1.ps1
function print
{
Write-Output "hello"
}
script2.ps1

Code: Select all

# script2.ps1
. "script1.ps1"
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
Site Admin
Posts: 260
Joined: Sun Mar 03, 2013 12:42 pm

Re: SAPIEN PowerShellStudio does not find functions in included files

Post by mxtrinidad » Wed Jun 20, 2018 10:16 am

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.

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

Re: SAPIEN PowerShellStudio does not find functions in included files

Post by jvierra » Wed Jun 20, 2018 10:23 am

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

Re: SAPIEN PowerShellStudio does not find functions in included files

Post by davidc » Wed Jun 20, 2018 10:53 am

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

Use this instead:

Code: Select all

. ".\script1.ps1"
David
SAPIEN Technologies, Inc.

User avatar
mxtrinidad
Site Admin
Posts: 260
Joined: Sun Mar 03, 2013 12:42 pm

Re: SAPIEN PowerShellStudio does not find functions in included files

Post by mxtrinidad » Wed Jun 20, 2018 11:04 am

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/

Locked