Can anyone explain Globals.ps1?

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 6 years and 3 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.
User avatar
trevor530
Posts: 25
Last visit: Mon Feb 12, 2024 9:27 am

Can anyone explain Globals.ps1?

Post by trevor530 »

Product, version and build: Powershell Studio 2017 Version 5.4.145
32 or 64 bit version of product: 64
Operating system: Windows 10
32 or 64 bit OS: 64

Can anyone explain Globals.ps1? Provide a link that has some documentation?

I have a multi-form project and I put some variables and functions in Globals.ps1, the Build and Shared properties of the file are set to Include and True respectively, the functions seem to behave as I expect but the variables declared there do not. When I add $global: in front of the variables they seem to work as expected but I thought the whole point of having Globals.ps1 was so you did not have to explicitly define the variables with a scope.
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: Can anyone explain Globals.ps1?

Post by Alexander Riedel »

Using a particular file name has no influence on the scope of the content. PowerShell has no facility for that.
When the resulting script gets generated, the variables and functions within this file are placed outside of any other scope, which basically defaults them to '$script:' scope. For all intends and purposes that is 'global' as far as everything else in your script goes.
If you run your scripts in a console and you need to modify or create variables outside of the script, you still need to precede them with the '$global:' modifier.
PowerShell does have some issues with scoping and it is not always transparent what a variable will default to. So as a best practice we generally recommend to always use the scope modifiers if you want to be sure.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Can anyone explain Globals.ps1?

Post by mxtrinidad »

May I add to this thread. I have use the Global.ps1 to store custom PowerShell functions I have created. This way I can reuse them across my multiple screens without any issues.

This has work great in my situation.
:)
User avatar
trevor530
Posts: 25
Last visit: Mon Feb 12, 2024 9:27 am

Re: Can anyone explain Globals.ps1?

Post by trevor530 »

I did notice that when the script is generated the variables in Global.ps1 did not have $script: scope assigned to them. I have played around with having no scope and scope. When in doubt I will just add the $script: scope myself.
This topic is 6 years and 3 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.