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.
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.
Keeping Variables in external file
- Attachments
-
- ExternalVariables.zip
- (48.16 KiB) Downloaded 115 times
Re: Keeping Variables in external file
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
help ConvertFrom-StringData -Full
We would use it like this:
Get-Content variables.txt | ConvertFrom-StringData
Re: Keeping Variables in external file
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?
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?
Re: Keeping Variables in external file
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)
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 }
Re: Keeping Variables in external file
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.
Maybe something like this:
$GlobalVariables | ForEach-Object { if ($_.keys -notcontains '#'){ New-Variable -Name $_.keys -Value $_.values -Scope Global} }
Gives an error but works.
Re: Keeping Variables in external file
Note: importing a share also gives issues
share=\\domain\whatever\ because of the escape character \ ....
what can be solved by \\\\domain\\whatever\\ ...
share=\\domain\whatever\ because of the escape character \ ....
what can be solved by \\\\domain\\whatever\\ ...
Re: Keeping Variables in external file
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)
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)