Powershell & XML: Save data to a certain node of xml-file -
i have powershell script loads data xml. xml looks like:
<meta> <log> <path>d:\logs\l1.log</path> <lastwrite>01/30/2015 13:01:00</lastwrite> <num>23</num> </log> <log> <path>d:\log\l2.log</path> <lastwrite>02/30/2015 14:02:00</lastwrite> <num>67</num> </log> </meta>
what change value in xml. first load data:
[xml]$xml = get-content "d:\config.xml"
my problem is, how can address log-node in xml? i'm searching this:
$xml.meta.log.path | path = "d:\log\l2.log"
and set new value , save xml-file.
maybe sb can me, have idea how search, i'm sure there's way. use ids within log-tags not solution, because have address nodes paths.
you there. instead of =
use -eq
condition. have use curly brackets , access current value using $_
:
$xml.meta.log.path | { $_ -eq 'd:\log\l2.log' }
alternative, can use where path
query on $xml.meta.log
, select path in additional statement:
$xml.meta.log | path -eq 'd:\log\l2.log' | select -expand path
and here complete example modifies path , save xml back:
$configpath = 'd:\config.xml' [xml]$xml = get-content $configpath # select log log node path equals 'd:\log\l2.log' $node = $xml.meta.log | path -eq 'd:\log\l2.log' # set new path $node.path = 'd:\newlogpath\newlog.log' # save xml $xml.save($configpath)
Comments
Post a Comment