9

How can i get variables from a config file in XML ?

I am actualy doing this using a .txt config file.

There is my actual code :

Config.txt

[Repertoire de SPO (generalement C:\SPO)]
destinationn="C:\SPO"

[Nom du sous repertoire de SPO (generalement : SPO1)]
sous_destination="SPO1"

[Numero de la version de SPO vers laquelle vous souhaitez upgrade (par exemple : 1812.4)]
version="1812.4"

[Nom du pool dapplication lie a SPO (par defaut SPO_ADV)]
applicationPoolName="SPO_ADV"

[Chemin de livraison de la nouvelle version de SPO (par defaut \\path\to\somewhere)]
livraisonn="\\path\to\somewhere"

Powershell.ps1

Get-Content "$current_path\config.txt" | foreach-object -begin {$h=@{}} -process { $k = [regex]::split($_,'='); if(($k[0].CompareTo("") -ne 0) -and ($k[0].StartsWith("[") -ne $True)) { $h.Add($k[0], $k[1]) } }

$destinationn = $h.Get_Item("destinationn")

I would like to do a similar thing using an .xml config file.

3
  • You may want to have a look at the Script Get-IniContent from the TechNet Gallery which enables you to "parse" an ini-like file to return the values as a Hashtable Commented Jan 4, 2019 at 10:55
  • If you are open to reading JSON ot INI (i.e. TXT) files, consider github.com/alekdavis/ConfigFile Commented May 6, 2019 at 6:18
  • There is a much more convenient way to read settings using the ConvertFrom-Json Cmdlet in PowerShell to read a JSON formatted file: serverfault.com/a/1032517/293696 Commented Sep 3, 2020 at 7:21

3 Answers 3

12

Xml is much easier to parse with PowerShell. See following example with comments:

#Translated XML config
@'
<root>
  <destinationn>C:\SPO</destinationn>
  <sous_destination>SPO1</sous_destination>
  <version>1812.4</version>
  <applicationPoolName>SPO_ADV</applicationPoolName>
  <livraisonn>\\path\to\somewhere</livraisonn>
</root>
'@ | Out-File YourSampleConfig.xml

#read xml file, skip root
$config = ([xml](Get-Content YourSampleConfig.xml)).root

#get <destinationn> value
$config.destinationn
Sign up to request clarification or add additional context in comments.

2 Comments

What is the "root" for here ?
root is main, required node in xml: <root>...<root>. Name it whatever you want. If you name it <ABC>...</ABC>, you must chage it to ABC in script as well.
3

Getting connection string from App.Config

$appConfigFile = [IO.Path]::Combine($currentDirectory, '.\MyFile.config')

initialize the xml object

$appConfig = New-Object XML

load the config file as an xml object

$appConfig.Load($appConfigFile)

iterate over the settings which you want

foreach ($connectionString in $appConfig.configuration.connectionStrings.add) {
    # Get the connection string
    $dbconnectionstring = $connectionString.connectionString
}

Here's my config xml

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="AtomicScopeConStr" connectionString="Server= localhost; Database= mydatabase; Integrated Security=True;" />
  </connectionStrings>
  <appSettings>
  </appSettings>
</configuration>

1 Comment

This was a great help. See the working code in the answer below. (which is slightly different from above). My config file was essentially identical to the one above. Same elements.
1

This is a follow up to HariHaran's post. My connection.config file had the same elements and structure as his, but my working code is slightly different.

Use this to load the xml object

$appConfig = New-Object XML
$appConfig.Load($appConfigFile)

or use this

[xml]$appConfig = Get-Content $appConfigFile

and here is the working code to get the connection string

foreach ($xmlAddElement in $appConfig.connectionStrings.add) {
    $connectionString = $xmlAddElement.connectionString
    Write-Host "$connectionString"
}

Comments

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.