Get back to the original file from a hashed xml table value

Hello,

I have a script that reads a bunch of xml files and stores some of the xml values in a hash table. (Later on this table is sent out in an email.)

The scriptblock is:

$hash = foreach($file in Get-ChildItem -Path $path){
    switch -Regex -File $file.fullname {
        '<logicalRecordIdentifier>(?<Identifier>.+?)(?=<)' {
            $id = $matches.Identifier
        }
        '<status>(?<Status>.+?)(?=<)' {
            [PSCustomObject]@{
                FileName   = $file.BaseName
                someID  = $id
                Status     = $matches.Status
            }
        }
    }
} 

If I want to collect all the files that had a status ‘Error’, how can I refer back to this hashtable?

I was trying to figure out something like :

$hash.GetEnumerator() |  Where-Object { $_.status -eq "Error"} | ForEach-Object {
Move-Item -Path $path - Destination $dest
 $i++
} 

It does not work as it moves everything, independently from the Status tag in the xml. Any idea what goes wrong?

PSrookie86,
Welcome back to the forum. :wave:t3:

Since a year has already passed in the meantime … Are you still a PowerShell rookie? :smirk: :wink:

Could you please go back, edit your question once again and fix the formatting of your ocde?

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Hello Olaf :slight_smile:

You’re right, I changed the formatting to highlight the codeblock.
As for being a rookie, well, the problem seems to be advanced for me but not sure if it is indeed that hard for other people

:+1:t3:

That was a joke because of your nick name here. :wink:

Just because you name something $hash it does not magically turn into a hashtable. In fact you’re explicitly creating a [PSCustomObject]. :man_shrugging:t3:

How does the output look like when you simply output the variable $hash to the console?

Regardless of that … if you have proper XML files I would mess with regex. Instead I’d use the built in ability to actually read the XML and turn it in to a hierarchical object structure.

I’d start with outputting the result of the Where-Object filter to the console before I pipe everything to a potentially risky cmdlet. :man_shrugging:t3: … or at least add a -WhatIf to the Move-Item command. :wink: