我有一个Powershell脚本,用于查询xml文档并将结果输出到csv文件。该脚本有效但我需要将其应用于文件夹中的多个xml文件,并将组合结果输出到csv。如何修改此脚本来执行此操作?感谢
$xml = [XML](Get-Content D:demo\test.xml) #load xml document
#this finds file names of yearbook picks
$picks = $xml.Client.Order.Ordered_Items.Ordered_Item |
Where-Object { $_.Description -eq 'yearbook Luster Print' } |
ForEach-Object { $_.Images.Image_Name }
# this finds the Album
$album = $xml.SelectSingleNode("//Album_ID").InnerText -split '_'
$results = New-Object PSObject -Property @{
Last= $album[0]
First= $album[1]
Code= $album[2]
Pick1= $picks[0]
Pick2= $picks[1]
}
#output CSV File
$results | Export-Csv -path D:\demo\myoutput.csv -NoTypeInformation
答案 0 :(得分:1)
循环遍历XML文件并将它们附加到输出文件中:
# Get all XML files
$items = Get-ChildItem *.xml
# Loop over them and append them to the document
foreach ($item in $items) {
$xml = [XML](Get-Content $item) #load xml document
#this finds file names of yearbook picks
$picks = $xml.Client.Order.Ordered_Items.Ordered_Item |
Where-Object { $_.Description -eq 'yearbook Luster Print' } |
ForEach-Object { $_.Images.Image_Name }
# this finds the Album
$album = $xml.SelectSingleNode("//Album_ID").InnerText -split '_'
$results = New-Object PSObject -Property @{
Last= $album[0]
First= $album[1]
Code= $album[2]
Pick1= $picks[0]
Pick2= $picks[1]
}
# Append to CSV File
$results | Export-Csv -path D:\demo\myoutput.csv -NoTypeInformation -Append
}