Pergunta

I have an config file from a NAVISION instance.

The file format is like this:

    <?xml version="1.0" encoding="UTF-8"?>
         <appSettings>
               <add key="NetType" value="Default"></add>
               <add key="DatabaseServer" value="SQLSERVER"></add>
               <add key="DatabaseInstance" value="MSSQL"></add>
               ......
         </appSettings>

I currently use this code:

[xml]$configfile = Get-Content "CustomSettings.config"
$appsettings = $configfile.appSettings.add.GetEnumerator()
Echo $appsettings

The end result that I get in Powershell is this:

key                                     value
---                                     -----
NetType                                 Default
DatabaseServer                          SQLSERVER
DatabaseInstance                        MSSQL
...                                     .....

However if I try to get for instance the DatabaseServer with $appsettings.'DatabaseServer' (or $appsettings.DatabaseServer) it will return nothing.

This works;

ForEach ($keypair in $appsettings) {
    echo $($keypair.Key + "    -    " + $keypair.Value);
}

But since I only need to have 5 values, I don't want to create an ForEach loop with 5 IFs.. there must be a better/faster way. Thanks in advance!

Foi útil?

Solução

Maybe

PS>($configfile.appSettings.add | ?{$_.key -eq "databaseserver"}).value        
SQLSERVER                                       

Outras dicas

If you're wanting a hash table:

[xml]$configfile = Get-Content "CustomSettings.config"
$appsettings = @{}
$configfile.appSettings.add.GetEnumerator() |
 foreach {$appsettings[$_.key] = $_.value}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top