xmlpowershellformattable

Getting an empty pipe error when trying to move format-table outside of loop


Trying to loop this code through multiple XML docs listed in a CSV, and not sure where to move my Format-Table without getting the An empty pipe element is not allowed error.

$CSV = Import-Csv "C:\Users\Megan\Documents\EC_Export\AllDocs.csv"

foreach($LINE in $CSV)
    {
    $docPath = $LINE.filepath

# Note: The following should be preferred over Get-Content which
#       doesn't respect XML encoding!

$xmlFile = [xml]::new(); $xmlFile.Load(( Convert-Path -LiteralPath $docPath ))

# Create an ordered hashtable as a precursor for a PSCustomObject
$ht = [ordered] @{}

# Process all ChildNodes
$xmlFile.files.file.ChildNodes |
    # Filter by Name property (which is either element name or Name attribute) (Can only do 10 fields at a time this way)
    Where-Object Name -match 'title|lcmSubject|lcmPrincipal|lcmClosingDate|lcmAclList' | 
    ForEach-Object {
        # Get element text, trim whitespace, replace any line breaks by comma.
        $value = $_.'#text' #.Trim() -replace '\r?\n', ',' 

        # Add hashtable entry (associate name with value)
        $ht[ $_.Name ] = $value 
    }

# Convert hashtable to a PSCustomObject so Format-Table prints it as expected.
[PSCustomObject] $ht} | Format-Table -Wrap

Solution

  • Here's a simplified version of a ForEach-Object solution:

    $CSV | ForEach-Object {
      # Note the need to use of $_ to refer to the input object at hand
      $docPath = $_.filepath
      # ... 
    } | Format-Table -Wrap