I have a script that is looking for some values in xml-like files:
$path = "C:\Users\*.xml"
Get-ChildItem $path |
Select-String -Pattern "<status>", "<logicalIdentifier>" | Out-File out.txt
The result is:
I am trying to store these in an array, however nothing happens (no error message on the console but nothing is printed either):
$path = "C:\Users\*.xml"
Get-ChildItem $path |
Select-String -Pattern "<status>", "<logicalRecordIdentifier>" |
Foreach-Object {
$id, $status = $_.Matches[0].Groups['<logicalIdentifier>', '<status>'].Value
[PSCustomObject] @{
ID = $id
Status = $status
}
}
I know it's a long shot, but not sure what goes wrong. Originally the XML file has the following sructure:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ACKReceipt xmlns="http://www.test.eu/ReceiptSchema_V1.xsd">
<receiptTimestamp>2021-04-23T20:32:09.239Z</receiptTimestamp>
<product>
<type>LXGF-H78</type>
</product>
<receiptType>validation</receiptType>
<validationReceipt>
<reportedFilename>filename1</reportedFilename>
<globalReceiptItem>
<logicalIdentifier>4567-YYYMMDD</logicalIdentifier>
<status>Accepted</status>
</globalReceiptItem>
</validationReceipt>
</ACKReceipt>
CodePudding user response:
This code works for your xml structure:
Clear-Host
$path = "C:\tmp\xml\*.xml"
$foundFiles = Get-ChildItem $path
$arrayTable = $null
$arrayTable = @()
foreach($file in $foundFiles) {
$file.FullName
[xml]$fileXML = Get-Content $file | Select-Object -Skip 1
$currentLogicalIdentifier = $fileXML.ACKReceipt.validationReceipt.globalReceiptItem.logicalIdentifier
$currentStatus = $fileXML.ACKReceipt.validationReceipt.globalReceiptItem.status
$arrayTable = new-object psobject -property @{Status=$currentStatus;LogicalIdentifier=$currentLogicalIdentifier;File=$file.Name}
}
# Report to csv file
$arrayTable | Export-Csv -Path "C:\tmp\xml\report.csv" -UseCulture -NoTypeInformation -Encoding "UTF8"
Here is how it looks like in eviroment table:
You can easy export it as csv file as a report.