Can anyone explain Globals.ps1?

Support for all customers who have purchased a SAPIEN PowerShell Studio product license. This forum does not offer a response time guarantee.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE 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.
User avatar
trevor530
Posts: 5
Joined: Tue Sep 22, 2015 2:52 pm

Can anyone explain Globals.ps1?

Post by trevor530 » Wed Dec 06, 2017 4:36 pm

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: 6361
Joined: Tue May 29, 2007 4:43 pm

Re: Can anyone explain Globals.ps1?

Post by Alexander Riedel » Wed Dec 06, 2017 6:38 pm

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
Site Admin
Posts: 45
Joined: Sun Mar 03, 2013 12:42 pm

Re: Can anyone explain Globals.ps1?

Post by mxtrinidad » Wed Dec 06, 2017 8:35 pm

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: 5
Joined: Tue Sep 22, 2015 2:52 pm

Re: Can anyone explain Globals.ps1?

Post by trevor530 » Thu Dec 07, 2017 9:01 am

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.