locked
Reading configuration values in PowerShell. RRS feed

  • Question

  • <configuration>
      <appSettings>
        <add key="SMTPServer" value="smtpserver/>
        <add key="MailFrom" value="admin@gmail.com"/>
        <add key="MailTo" value="abc@gmail.com,cde@gmail.com"/>
        <add key="Subject" value="Suspended Messages - "/>
        <add key="AppName" value="testApplication1, testApplication2"/>
      </appSettings>
    </configuration>


    above is the config file I have I am not finding best solution to read these keyvalues.
    Also to get multiple MailTo addresses and applications

    How can I read these values in powershell ?

    I tried below code

    $xml = [xml] (Get-Content "D:\TestSettings.config")
    [STRING]$PSEmailServer =$xml.SelectNodes('//add[@key="SMTPServer"]/@value')[0].'#text' 
    [STRING]$SubjectPrefix =$xml.SelectNodes('//add[@key="Subject"]/@value')[0].'#text'    
    [STRING]$From = $xml.SelectNodes('//add[@key="MailFrom"]/@value')[0].'#text'           
    [array]$EmailTo = $xml.SelectNodes('//add[@key="MailTo"]/@value')[0].'#text'   
    [array]$App = $xml.SelectNodes('//add[@key="AppName"]/@value')[0].'#text' 
    Tuesday, November 7, 2017 6:14 AM

Answers

  • Very easy:

    $xml.SelectSingleNode('//add[@key="SMTPServer"]').Value
    $xml.SelectSingleNode('//add[@key="Subject"]').Value
    $xml.SelectSingleNode('//add[@key="MailTo"]').Value
    


    \_(ツ)_/

    Tuesday, November 7, 2017 6:27 AM

All replies

  • Very easy:

    $xml.SelectSingleNode('//add[@key="SMTPServer"]').Value
    $xml.SelectSingleNode('//add[@key="Subject"]').Value
    $xml.SelectSingleNode('//add[@key="MailTo"]').Value
    


    \_(ツ)_/

    Tuesday, November 7, 2017 6:27 AM
  • Thank you, Thank you, THANK YOU!!!

    Been pulling my hair out over this, trying to find how to read the value of an add key line in a web.config.  Had no problems reading the file into an XML variable, modifying stuff in it and writing it out.  But a simple read of an addkey line was driving me nuts.

    Wish I could mark the above as the answer, 'cause it definitely is!!!


    Saturday, October 5, 2019 12:39 AM
  • Here's another way:

    $xml.configuration.appsettings.add.where{$_.key -eq 'smtpserver'}.value
    $xml.configuration.appsettings.add.where{$_.key -eq 'subject'}.value
    $xml.configuration.appsettings.add.where{$_.key -eq 'mailto'}.value -split ','

    • Edited by JS2010 Saturday, October 5, 2019 3:48 PM
    Saturday, October 5, 2019 3:45 PM
  • Here's another way:

    $xml.configuration.appsettings.add.Where{$_.key -eq 'smtpserver'}.value
    $xml.configuration.appsettings.add.Where{$_.key -eq 'subject'}.value
    $xml.configuration.appsettings.add.Where{$_.key -eq 'mailto'}.value

    This method works but only under very limited circumstances and assumes the example is the actual and complete config file.  Most config files can be much deeper and sections may contain similar sections.  If the file path is exact and known then it is easy but consider the following.

    The problem here is that "configuration" is contained inside two other layers of nodes so you have to know the complete path from the root. With "SelectSingleNode" we only need the closest unique part of the path.

    Also with XML methods we can use wildcards to incrementally search.

     $xml.SelectSingleNode('//*/source/*/add[@name="File"]')

    We can also do targeted compound searches.

    $xml.SelectSingleNode('//*/source[@name="Executive"]/*/add[@name="File"]')

    Think of XPath and XQuery as SQL for XML.


    \_(ツ)_/

    Saturday, October 5, 2019 4:13 PM