Keeping Variables in external file

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
This topic is 6 years and 6 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.
Locked
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Keeping Variables in external file

Post by jvierra »

Example:

+++ PS1 file vartest.ps1 +++
$global:myvar1 = 'hello;
++++ end +++


At PS prompt:
rv myvar
. .\vartest.ps1
$myvar


The result "hello" will be printed.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Keeping Variables in external file

Post by stevens »

? 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Keeping Variables in external file

Post by jvierra »

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.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Keeping Variables in external file

Post by stevens »

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.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Keeping Variables in external file

Post by stevens »

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?
Attachments
Load-ExternalVariables.zip
(29.93 KiB) Downloaded 130 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Keeping Variables in external file

Post by jvierra »

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
	}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Keeping Variables in external file

Post by jvierra »

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
	
}
cody m

Re: Keeping Variables in external file

Post by cody m »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Keeping Variables in external file

Post by jvierra »

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.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Keeping Variables in external file

Post by stevens »

Thanks for your input guys. Figured it out meanwhile!
This topic is 6 years and 6 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.
Locked