Reputation:
I have a Power Shell script that queries an xml document and outputs the results to a csv file. The script works, but I need to apply it to multiple xml files in a folder, then output the results as a separate csv for each XML with the same XML name. How can this script be modified to do this?
PROCESS
{
$xml = [xml](Get-Content .\sample.xml) #Sample XML
$xml.log.event | Select-object @(
@{l="times";e={$_.time}},
@{l="type";e={$_.type}},
@{l="user";e={$_.user}},
@{l="place";e={$_.place}},
@{l="message";e={$_.message}}) |
Export-Csv test.csv -NoTypeInformation
}
Upvotes: 1
Views: 669
Reputation: 19684
Assuming a directory:
$ToProcess = Get-ChildItem -Path 'C:\Temp' -Filter '*.xml'
ForEach ($File in $ToProcess)
{
[Xml]$Xml = Get-Content -Path $File.FullName
$Xml.log.event | Select-Object -Property @(
@{Name='Times';Expression={$_.Time}},
@{Name='Type'; Expression={$_.Type}},
@{Name='User'; Expression={$_.User}},
@{Name='Place';Expression={$_.Place}},
@{Name='Message';Expression={$_.Message}}
) | Export-Csv -Path "C:\Temp\$($File.BaseName).csv" -NoTypeInformation
}
Upvotes: 2