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!

有帮助吗?

解决方案

Maybe

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

其他提示

If you're wanting a hash table:

[xml]$configfile = Get-Content "CustomSettings.config"
$appsettings = @{}
$configfile.appSettings.add.GetEnumerator() |
 foreach {$appsettings[$_.key] = $_.value}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top