Powershell‎ > ‎

XML Config file

Found a nice article on how to load your config from an XML file

http://www.bjd145.org/2008/01/powershell-and-xml-configuration-files.html


Load config from XML file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <QueueServer name="blah"/>
 
</configuration>



$cfg =[xml](Get-Content "C:\scripts\PowerShell\QueueServers.xml")
$servername = $cfg.configuration.QueueServer.name

Load and modify XML file

Another example from stackoverflow

http://stackoverflow.com/questions/91791/grep-and-sed-equivalent-for-xml-command-line-processing

<root>
  <one>I like applesauce</one>
  <two>You sure bet I do!</two>
</root>


# load XML file into local variable and cast as XML type.
$doc = [xml](Get-Content ./test.xml)

$doc.root.one                                   #echoes "I like applesauce"
$doc.root.one = "Who doesn't like applesauce?"  #replace inner text of <one> node

# create new node...
$newNode = $doc.CreateElement("three")
$newNode.set_InnerText("And don't you forget it!")

# ...and position it in the hierarchy
$doc.root.AppendChild($newNode)

# write results to disk
$doc.save("./testNew.xml")

<root>
  <one>Who likes applesauce?</one>
  <two>You sure bet I do!</two>
  <three>And don't you forget it!</three>
</root>




Comments