Find matching node in XML and Get its Contents
i trying data folder contains several other xml files. script supposed take string csv, through these folders , return xml contains string csv.
right code gives me error , whole bunch of blank lines.
cannot convert value "" type "system.xml.xmldocument". error:"the specified node cannot inserted valid child of node, because specified node wrong type."
but not trying replace data
this csv
my csv looks like
key,stigs v-2258, apache site 2 v-2258, windows 7
my code returns "apache site 2"
so if have file1.xml, file2.xml, file3.xml.
file2 has node word "apache site 2" select file , can pull data it.
i dumbed xml file down lot section think need this:
<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type='text/xsl' href='stig_unclass.xsl'?> <benchmark"> <title>apache site 2</title>
<reference>other information</reference> <group id="v-2258"> <check-content>random information</check-content> </group> </benchmark>
this code:
$key = "insert key"
$csv = import-csv "file.csv" $firststig = (($csv | {$_.key -eq $key }).stigs) | select -first 1 $dir = "path" $xmlfiles = get-childitem $dir -filter *.xml | foreach{ $file = $_.name [xml]$xmldata = get-content "path\$file" $xmldata = $xmldata | where-object {$xmldata.benchmark.title -contains $firststig} $xmldata = $xmldata.benchmark.title write-host $xmldata }
any advice or appreciated.
without actual xml may hard show how do.
[xml]$xmldata=@' <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type='text/xsl' href='stig_unclass.xsl'?> <benchmark> <title>apache site 2</title> <reference>other information</reference> <group id="v-2258"> <check-content>random information</check-content> </group> </benchmark> '@ $firststig= 'apache site 2' $node = $xmldata.selectsinglenode("//benchmark/title[contains('$firststig',text())]") $node.'#text' \_(ツ)_/
Windows Server > Windows PowerShell
Comments
Post a Comment