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

Keeping Variables in external file

Post by stevens »

Hi,

Another attempt to figure out howto store variables in an external file (PS Studio).
Unfotunately, the other post is locked: viewtopic.php?f=21&t=11521 before I could comment that I can't make it work.

To make it clear, I made a project which shows what I'm trying to do.
Please advise.
J.
Attachments
ExternalVariables.zip
(48.16 KiB) Downloaded 115 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 »

I suggest that you start by reading the following carefully:

help ConvertFrom-StringData -Full

We would use it like this:

Get-Content variables.txt | ConvertFrom-StringData
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 »

Oh thanks, works indeed. Thanks!

textfile contains:
server01=Juno

form load is then:
$form1_Load={
#TODO: Initialize Form Controls here
$textdata = Get-Content "$ScriptDirectory\variables.txt"
$settings = $TextData | ConvertFrom-StringData

$textbox1.text = $settings.server01
}

Though it would be nice to just have $server01 instead of $settings.server01.
Can I format the textfile further, like commenting, adding spaces etc or is it limited to the format variable=value?
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 »

In the old thread I showed you how to do this with excellent detail.
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 »

Right indeed. How can I declare these variables as global?
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 »

$data.Keys | %{Set-Variable -Name $_ -Value $data[$_] -Scope Global}

You must take time out to learn how to use PowerShell help:

help Set-Variable -online
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 »

For future reference, this is what works for me:
textfile contains
server01=juno
server02=...

Then in globals.ps1 ($scriptdirectory = get-scriptdirectory should of course be there)

Code: Select all

$textdata = Get-Content "$ScriptDirectory\variables.txt"
$settings = $TextData | ConvertFrom-StringData
$settings | ForEach-Object{ New-Variable -Name $_.keys -Value $_.values -Scope Global }
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 »

The only thing I'm missing now is howto add comment into the textfile and PS Studio ignoring those lines during import.
Maybe something like this:

$GlobalVariables | ForEach-Object { if ($_.keys -notcontains '#'){ New-Variable -Name $_.keys -Value $_.values -Scope Global} }

Gives an error but works.
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 »

Note: importing a share also gives issues
share=\\domain\whatever\ because of the escape character \ ....
what can be solved by \\\\domain\\whatever\\ ...
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 all does work but it does not give the possibility to make the variables flexible.
F.e. variables with multiple values, variables which contain other variables ...
Isn't there a way to convert my variables (see below) to a Powershell Studio importable file?

$Global:RDSADGroupNames = @{
GroupUsersCompany01='ADGroup01'
GroupUsersCompany02='ADGroup02
}
$Global:Domain = $env:USERDNSDOMAIN
$Global:Organization = 'OurOrganization'
$Global:SCCMSiteServer01 = 'oursccmserver'
$Global:SCCMSiteServer01_FQDN = ($SCCMSiteServer01 + '.' + $Domain)
This topic is 6 years and 5 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