Find matching node in XML and Get its Contents

This topic contains 1 reply, has 2 voices, and was last updated by  Jonathan Warnken 2 years, 2 months ago.

  • Author
  • #38391

    Long S

    I am trying to get data from a folder that contains several other xml files. My script is supposed to take a string from a csv, look through these folders and return the xml that contains my string from the csv.

    Right now my code gives me a whole bunch of blank lines.
    My CSV looks like

    V-2258, APACHE SITE 2
    V-2258, Windows 7

    My code returns "APACHE SITE 2" for $firststig

    So if I have file1.xml, file2.xml, file3.xml.
    file2 has a node with the word "APACHE SITE 2" it will select that file and I can pull data from it.

    This is my code:
    $key = "Insert Key" # the user is prompted for 2258#
    $csv = Import-CSV "file.csv"
    $firststig = (($csv | where {$_.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}
    write-host $xmldata.Benchmark.title

    im not sure how to insert my sample from my xml file into this thread.

    I believe $xmldata = $xmldata | Where-Object {$xmldata.Benchmark.title -contains $firststig} is the issue.

  • #38394

    Jonathan Warnken

    I setup the following to test this

    and used this powershell to display the xml when it contained string in the csv file

    $key ="V-2258" # the user is prompted for 2258#
    $csv = Import-CSV D:\test\test.csv
    $firststig = (($csv | where {$_.KEY -eq $key }).STIGS) | select -first 1
    $dir = "D:\test"
    $xmlfiles = Get-ChildItem $dir -Filter *.xml | foreach{
        [xml]$xmldata = Get-Content -Path $_.FullName
        if($xmldata.Benchmark.title -contains $firststig){
        write-host $xmldata.Benchmark.title

You must be logged in to reply to this topic.