Correctly Enumerating through a Hashtables values in a ForEach-Object Loop

Firstly the Hashtable (Called $MyHashTable) looks like this:

Name                           Value
----                           -----
NTER 1                         {[Short, Kill > Dump], [Long, Procces is terminated, then all errors are dumped to a log file1]}
NTER 2                         {[Short, Kill > Dump], [Long, Procces is terminated, then all errors are dumped to a log file2]}
NTER 3                         {[Short, Kill > Dump], [Long, Procces is terminated, then all errors are dumped to a log file3]}
NTER 4                         {[Short, Kill > Dump], [Long, Procces is terminated, then all errors are dumped to a log file4]}
LA2 1                          {[Short, Kill > Dump], [Long, Procces is terminated, then all errors are dumped to a log file5]}
LA2 2                          {[Short, Kill > Dump], [Long, Procces is terminated, then all errors are dumped to a log file6]}
LA2 3                          {[Short, Kill > Dump], [Long, Procces is terminated, then all errors are dumped to a log file7]}
LA2 4                          {[Short, Kill > Dump], [Long, Procces is terminated, then all errors are dumped to a log file8]}

I want to use this Hashtable in a ForEach-Object loop, where $MyHashTable.Name will be used as a name for the file in the loop, and $MyHashTable.values.long will be used as MetaData for this same file. The output should be:

File Name MetaData “Misc1”
NTER 1.svg Procces is terminated, then all errors are dumped to a log file1
NTER 2.svg Procces is terminated, then all errors are dumped to a log file2
NTER 3.svg Procces is terminated, then all errors are dumped to a log file3
NTER 4.svg Procces is terminated, then all errors are dumped to a log file4
…So on … So on
In a my file manager:

Here is a snippet of my code (the part where the files are being generated):

    $MyHashTable.keys | ForEach-Object {

        # First Copy the files to their folder

        Copy-item -path 'C:\Temp\Template 2.svg' -Destination "C:\Temp\$_.svg"

        foreach ($Key in $MyHashTable.values.long) {

            # Then Populate their Column Data

            set-metadata  -path "C:\Temp\$_.svg" -Misc1 $key}
            }

The files copy fine with the desired names but foreach ($Key in $MyHashTable.values.long) loop just gives every file the same MetaData Value (“Procces is terminated, then all errors are dumped to a log file1”
Each of these commands and expressions (Set-MetaData, $MyHashTable.values.long etc ) work just fine indvidually on their own. Just putting them together…

I’ve litterally been at this for hours and tried a number of things: diagnosing the issue, alternatives etc I cannt get around. So close… Please help!


I’ve taken the liberty to cross post this question other forums. Thanks!

Which ones? Links? Is it maybe already solved there?

For now - until you provide the links … just a a hint … you’re using a nested loop but in the inner loop you’re not refering to the single element from the outer loop. :wink:

It was resolved here.

2 Likes

Thanks. :+1:t4: I’ve seen that … of course. :wink: I just thought it would have been nice from Hellen to follow common rules and not overuse the kindness of people willing to help. :man_shrugging:t4: I wonder anyway why it often takes such a long time for many people to answer to replies they get in forums like this. Sometimes I have the feeling they ask their questions and leave the office for the holidays right away. :roll_eyes:

1 Like

Thanks much appreciated.
Eddit: replied to the wrong person. :face_with_hand_over_mouth:
I meant @krzydoug