-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_xml.ps1
38 lines (29 loc) · 1.11 KB
/
extract_xml.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Define the directory path
$directoryPath = "C:\Path\To\XML\Files"
# Output CSV path
$outputCsv = "C:\Path\To\Output\BuyersOrderNumbers.csv"
# Create an empty array to store the results
$buyersOrderNumbers = @()
# Loop through all XML files in the directory
Get-ChildItem -Path $directoryPath -Filter *.xml | ForEach-Object {
$xmlFile = $_.FullName
# Load the XML content
try {
[xml]$xmlContent = Get-Content -Path $xmlFile
# Extract the value of <BuyersOrderNumber>
$buyersOrderNumber = $xmlContent.SelectSingleNode("//BuyersOrderNumber").InnerText
# Output the value
Write-Output "Buyers Order Number: $buyersOrderNumber from file: $($_.Name)"
# Store the result in the array
$buyersOrderNumbers += [pscustomobject]@{
FileName = $_.Name
BuyersOrderNumber = $buyersOrderNumber
}
}
catch {
Write-Output "Failed to read or parse $($_.Name)"
}
}
# Export results to CSV
$buyersOrderNumbers | Export-Csv -Path $outputCsv -NoTypeInformation
Write-Output "Results exported to $outputCsv"