none
Extracting the value from a single key in an XML file RRS feed

  • Question

  • I have been trying to get a value from a single key in an XML file but not having much luck.

    The XML looks like this:

    <appSettings>
        <!-- Comment about keys -->
        <add key="stateServers" value="STATE-01:46424,STATE-02:46424"/>
        <add key="pingRequestTimeout" value="2000"/>
        <add key="stateServerRetrySeconds" value="120"/>
        <add key="IPWhiteList" value="192.168.0.1,192.168.0.2,192.168.0.3,10.9.0.1,10.9.0.51,10.7.9.9"/>
        <add key="Environment" value="TEST" />
        <add key="Services" value="net.tcp://service-01:8090/Services"/>
    </appSettings>

    I want to get just the IPWhiteList key into a variable.

    I have tried a few different ways but so far I am not having any luck.

    [xml]$inputFile = Get-Content "C:\file\appSettings.config"
    $IPWhitelist = $inputFile.appSettings.IPWhitelist
    write-host $IPWhitelist

    Using childnodes works in that I get every key, but I am looking to only get the value of the IPWhiteLisy key.

    Thursday, April 18, 2019 11:39 AM

Answers

  • $inputFile.SelectSingleNode("//add[@key='IPWhiteList']").Value
    • Edited by Leif-Arne Helland Thursday, April 18, 2019 12:10 PM
    • Proposed as answer by jrv Thursday, April 18, 2019 12:16 PM
    • Marked as answer by R-Cook Thursday, April 18, 2019 2:19 PM
    Thursday, April 18, 2019 12:10 PM

All replies

  • $inputFile.SelectSingleNode("//add[@key='IPWhiteList']").Value
    • Edited by Leif-Arne Helland Thursday, April 18, 2019 12:10 PM
    • Proposed as answer by jrv Thursday, April 18, 2019 12:16 PM
    • Marked as answer by R-Cook Thursday, April 18, 2019 2:19 PM
    Thursday, April 18, 2019 12:10 PM
  • $ipwhitelist = $inputFile.appSettings.add | where key -eq ipwhitelist |
      select -expand value         
    $ipwhitelist                                                                                         
    192.168.0.1,192.168.0.2,192.168.0.3,10.9.0.1,10.9.0.51,10.7.9.9

    Sticking with powershell instead of xpath.  Use -split ',' if you want an array.

    • Edited by JS2010 Thursday, April 18, 2019 8:52 PM
    Thursday, April 18, 2019 12:27 PM