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.
User avatar
stevens
Posts: 307
Joined: Wed Jun 15, 2016 2:26 am

Keeping Variables in external file

Post by stevens » Wed Sep 13, 2017 9:16 am

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 7 times

User avatar
jvierra
Posts: 11479
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Keeping Variables in external file

Post by jvierra » Wed Sep 13, 2017 10:49 am

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: 307
Joined: Wed Jun 15, 2016 2:26 am

Re: Keeping Variables in external file

Post by stevens » Wed Sep 13, 2017 12:19 pm

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?

User avatar
jvierra
Posts: 11479
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Keeping Variables in external file

Post by jvierra » Wed Sep 13, 2017 12:25 pm

In the old thread I showed you how to do this with excellent detail.

User avatar
stevens
Posts: 307
Joined: Wed Jun 15, 2016 2:26 am

Re: Keeping Variables in external file

Post by stevens » Thu Sep 14, 2017 8:24 am

Right indeed. How can I declare these variables as global?

User avatar
jvierra
Posts: 11479
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Keeping Variables in external file

Post by jvierra » Thu Sep 14, 2017 9:51 am

$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: 307
Joined: Wed Jun 15, 2016 2:26 am

Re: Keeping Variables in external file

Post by stevens » Fri Sep 15, 2017 6:39 am

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: 307
Joined: Wed Jun 15, 2016 2:26 am

Re: Keeping Variables in external file

Post by stevens » Fri Sep 15, 2017 6:55 am

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: 307
Joined: Wed Jun 15, 2016 2:26 am

Re: Keeping Variables in external file

Post by stevens » Fri Sep 15, 2017 7:39 am

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: 307
Joined: Wed Jun 15, 2016 2:26 am

Re: Keeping Variables in external file

Post by stevens » Fri Sep 15, 2017 8:37 am

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)

Locked