2

I have the following script that I have to input manually.

However, I am looking to provide a text file along with the script to read the inputs from there.

For example, I have two variable below:

$RedirectURIs = $CustomerWebUIurl
$appURI = $CustomerSamlIssuerID

Where I need to pull the following values from text file

$CustomerWebUIurl
$CustomerSamlIssuerID

Screenshot of the config file text

So when I add

$configFile = Get-Content -Path .\config.conf

What else I can use/add to define those two lines in the text file to the two variables I have in the script?

Thanks for the help!

0

2 Answers 2

1

Your config file is in a format that the ConvertFrom-StringData can process, which converts lines of =-separated key-value pairs into hashtables:

# Create a sample config.conf file
@'
$CustomerWebUIurl = http://example.org&1
$CustomerSamlIssuerID = http://example.org&2
'@ > ./config.conf

# Load the key-value pairs from ./config.conf into a hashtable:
$config = ConvertFrom-StringData (Get-Content -Raw ./config.conf)

# Output the resulting hashtable
$config

The above yields:

Name                           Value
----                           -----
$CustomerWebUIurl              http://example.org&1
$CustomerSamlIssuerID          http://example.org&2

That is, $config now contains a hashtable with entries whose key names are - verbatim - $CustomerWebUIurl and $CustomerSamlIssuerID, which you can access as follows: $config.'$CustomerWebUIurl' and $config.'$CustomerSamlIssuerID'

The need to quote the keys on access is somewhat cumbersome, and the fact that the key names start with $ can be confusing, so I suggest defining your config-file entries without a leading $.

If you have no control over the config file, you can work around the issue as follows:

# Trim the leading '$' from the key names before converting to a hashtable:
$config = ConvertFrom-StringData ((Get-Content -Raw .\config.conf) -replace '(?m)^\$')

Now you can access the entries more conveniently as $config.CustomerWebUIurl and $config.CustomerSamlIssuerID

Sign up to request clarification or add additional context in comments.

Comments

0

I just figured out how, to use simply array:

Here is the answer script

$configFile = Get-Content -Path .\config.conf

$configFile.GetType()
$configFile.Count
$configFile
$CustomerWebUIurl = $configFile[0]
$CustomerWebUIurl

$CustomerSamlIssuerID = $configFile[1]
$CustomerSamlIssuerID

3 Comments

Respectfully, and maybe I don't understand the problem correctly, but doesn't this simply assign the string line from the file to a variable. Unless you change the values from the file so they are simply values and not PowerShell assignment statements. BTW other ways to handle this would have been to dot source the file or read the file and invoke the expressions it contains...
If your txt file looks exactly as shown then here you're storing the URL and the variable name. In addition, it would probably be safer to use Select-String or Where-Object or some other means of filtering for the variable assignment.
If you really wanted to read the file's lines as-is, you can simplify your solution via a multi-assignment: $CustomerWebUIurl, $CustomerSamlIssuerID = Get-Content .\config.conf. To process the file as a properties file composed of key-value pairs (variable/property name-value pairs), use ConvertFrom-StringData

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.