Get data from elements in several XML files for output to another, one XML file using Powershell

I'll start by recognizing that I'm Powershell (and coding) noob. I came across several scenarios, but I do not pretend to anything, even approaching competence. I hope some more experienced people can put me on the right track.

I am trying to pull specific element data from several XML files that will be used to populate another XML file. The files from which I extract the data are invoices, and I would like to get the account number and time stamp and drop these values ​​into the manifest. The structure of the manifest is as follows

<?xml version="1.0" encoding="utf-8"?>
<Manifest>
    <Invoice>
        <InvoiceID></InvoiceID>
        <Timestamp></Timestamp>
    </Invoice>
</Manifest>

The XML I am pulling from is in a subdirectory of the directory in which the manifest will be saved. For simplicity, the element names in the invoices are identical to the corresponding manifest elements. The folder structure for the manifest is " C:\Projects\Powershell\Manifest\Manifest.xml", and for invoices it is " C:\Projects\Powershell\Manifest\Invoices\*.xml".

"InvoiceID" "Timestamp" XML "\Invoices". ; , . (, , XML - "\Invoices", : <Invoice>, InvoiceID Timestamp, , , , .)

:

$files = Get-ChildItem "C:\Projects\Powershell\Manifest\Invoices\*.xml"

$xmlData = @"
    <Invoice>
        <InvoiceId>$InvID</InvoiceId>
        <Timestamp>$Timestamp</Timestamp>
    </Invoice>
"@
$Manifest = "C:\Projects\Powershell\Manifest\Manifest.xml"

ForEach ($file in $files) {
    $xmldoc = [xml](Get-Content $file)
    $InvID = $xmldoc.Manifest.Invoice.InvoiceID
    $Timestamp = $xmldoc.Manifest.Invoice.Timestamp
    ForEach ($xml in $xmldoc)
{
    Add-Content $Manifest $xmlData
}}

, .

, , , , , , , . /?

+4
1

() "..." @"<newline>...<newline>"@ , , , .
, , foreach.

, , , $ExecutionContext.InvokeCommand.ExpandString():

# Define the *template* string as a *literal* - with *single* quotes.
$xmlData = @'
    <Invoice>
        <InvoiceId>$InvID</InvoiceId>
        <Timestamp>$Timestamp</Timestamp>
    </Invoice>
'@

 # ...
 # ForEach ($file in $files) { ...
   # Perform interpolation *on demand* with $ExecutionContext.InvokeCommand.ExpandString()
   Add-Content $Manifest -Value $ExecutionContext.InvokeCommand.ExpandString($xmlData)
 # }

:

# Define a template string, *single-quoted*, with *literal contents*:
#  - '$InvID' is simply literally part of the string, not a variable reference (yet).
#  - Ditto for $((Get-Date).TimeOfDay)
$strTempl = 'Invoice ID $InvID extracted at $((Get-Date).TimeOfDay).'

# Echo the template string as-is - unexpanded - ...
$strTempl

# ... and expand it on demand
$InvID = 1
$ExecutionContext.InvokeCommand.ExpandString($strTempl)

# ... and again, after assigning a different value to $InvID
$InvID = 2
$ExecutionContext.InvokeCommand.ExpandString($strTempl)

- :

Invoice ID $InvID extracted at $((Get-Date).TimeOfDay).  # template literal
Invoice ID 1 extracted at 11:38:12.2719300.              # first on-demand expansion
Invoice ID 2 extracted at 11:38:12.2766010.              # second on-demand expnsion
+2

Source: https://habr.com/ru/post/1696231/


All Articles