Example:
+++ PS1 file vartest.ps1 +++
$global:myvar1 = 'hello;
++++ end +++
At PS prompt:
rv myvar
. .\vartest.ps1
$myvar
The result "hello" will be printed.
Keeping Variables in external file
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.
Re: Keeping Variables in external file
? this is just basic powershell.
Of course I'm talking about using PS variables in PS Studio. There, it does not work (using scriptpath, dotscouring the ps1, then loading the variable in f.e. a textbox $textbox1.text = $variableofps1
Of course I'm talking about using PS variables in PS Studio. There, it does not work (using scriptpath, dotscouring the ps1, then loading the variable in f.e. a textbox $textbox1.text = $variableofps1
Re: Keeping Variables in external file
No idea what you are trying to say. There is nothing different about a form. PSS doesn't change that unless there is a bug in the loading. This would then cause an error. You cannot load anything in a job and have it available outside of the job.
Re: Keeping Variables in external file
I'm not talking about a job. I'm just trying loading variables out of an (not PS studio) external file (txt, ps1, psm1, xml does not matter). This so I can share my form in the open and users can fill in their variables like $domain = 'ourdomain', $smtpserver = 'oursmtpserver' etc. It works fine with a txt file then
$textdata = Get-Content "$ScriptDirectory\globalvariables.txt"
$GlobalVariables = $TextData | ConvertFrom-StringData
but then I'm really limited with the textfile whereas with a ps1 I could clearly document what each variable means and keep it better organized. The txt-file has lots of limitations like f.e. share = \\\\server\\share\\...
I'll upload an example of what I mean if this is not clear.
$textdata = Get-Content "$ScriptDirectory\globalvariables.txt"
$GlobalVariables = $TextData | ConvertFrom-StringData
but then I'm really limited with the textfile whereas with a ps1 I could clearly document what each variable means and keep it better organized. The txt-file has lots of limitations like f.e. share = \\\\server\\share\\...
I'll upload an example of what I mean if this is not clear.
Re: Keeping Variables in external file
Well I be damend.
Example in att. works ....
But it does not in my form.
It DOES when I use
cd $ScriptDirectory\
.\GlobalVariables.ps1
but when I use . "$ScriptDirectory\GlobalVariables.ps1" it says it cannot execute the command. Maybe my path is to long?
Example in att. works ....
But it does not in my form.
It DOES when I use
cd $ScriptDirectory\
.\GlobalVariables.ps1
but when I use . "$ScriptDirectory\GlobalVariables.ps1" it says it cannot execute the command. Maybe my path is to long?
- Attachments
-
- Load-ExternalVariables.zip
- (29.93 KiB) Downloaded 130 times
Re: Keeping Variables in external file
The following works fine for me:
Code: Select all
$form1_Load={
$scriptpath = if ($hostinvocation) {
Split-Path $hostinvocation.MyCommand.path
} else {
Split-Path $script:MyInvocation.MyCommand.Path
}
. $scriptpath\myvariables.ps1
$textbox1.Text = $global:myvar
}
Re: Keeping Variables in external file
This also works for me with no issues:
Code: Select all
$form1_Load={
$textdata = Get-Content $ScriptDirectory\variables.txt |
ConvertFrom-StringData |
ForEach-Object { New-Variable -Name $_.keys -Value $_.values -Scope Global }
. $ScriptDirectory\variables.ps1
$textbox1.Lines = $sccmserver, $smtpserver
$textbox2.Lines = $sccmserverps1, $smtpserverps1
}
Re: Keeping Variables in external file
Your code works for me as long as the "globalvariablestest.ps1" file is in the correct directory and is named correctly. If the file is not named correctly an error occurs which says that the term is not recognized as the name of a cmdlet, function, script, file, or operable program.
Re: Keeping Variables in external file
The file must be named correctly and be in the script folder. That is the whole point of this thread.
If you have an issue that is different please open a new question with a clear statement of your issue.
If you have an issue that is different please open a new question with a clear statement of your issue.