Hello,
I have a powershell studio project with GUI that is giving me a headache.
In my global.ps1, I tried setting a global var with a value. Anywhere I try to reference that, the value is empty.
I tried, in global.ps1, using all of these:
$global:var1 = "asdf"
$script:var1 = "asdf"
$var1 = "asdf"
Then, in my other form, I try getting the value
$var1
$global:var1
$script:var1
All of them return null value... What am I doing wrong?
Thank you
Unable to use global var
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.
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.
- dan.potter
- Posts: 709
- Last visit: Wed Nov 14, 2018 11:39 am
Re: Unable to use global var
It's $global:var in the globals.ps1 and $var in the child form.
You don't have to use it in globals.ps1, you can stick it in the main form as well.
You don't have to use it in globals.ps1, you can stick it in the main form as well.
Re: Unable to use global var
In a clean project:
in globals add a line after the existing following line
In the "mainform.psf" add a textbox and add the following:
Now just hit the big green run button and you will see that value added in the "globals,ps1" file.
in globals add a line after the existing following line
- #Sample variable that provides the location of the script
- [string]$ScriptDirectory = Get-ScriptDirectory
- # add the following line
- $myvar='ABC12345'
- $MainForm_Load={
- #TODO: Initialize Form Controls here
- $textbox1.Text=$myvar
- }
Re: Unable to use global var
Hello,
I've tried like you said and it doesn't work. It's in a project and I run the full project.
I think I found the problem though, I'm trying to read the global value from a thread I'm creating. I've found out in the past that I must reinitialize everything in the thread, the context isn't copied (like import-module for example). That must be the problem.
I've tried like you said and it doesn't work. It's in a project and I run the full project.
I think I found the problem though, I'm trying to read the global value from a thread I'm creating. I've found out in the past that I must reinitialize everything in the thread, the context isn't copied (like import-module for example). That must be the problem.
Re: Unable to use global var
You cannot access any variables between threads (runspaces) as they are in isolated memory spaces. You must use a sync obkect.STM001 wrote:Hello,
I've tried like you said and it doesn't work. It's in a project and I run the full project.
I think I found the problem though, I'm trying to read the global value from a thread I'm creating. I've found out in the past that I must reinitialize everything in the thread, the context isn't copied (like import-module for example). That must be the problem.
The code I posted works if you actually follow the instructions. Adding an issue about threads does not change your original question. It just makes it a badly asked question.
Try to re-ask the question with the full details of your issue.