Home > Back-end >  Getting all the values of a hashtables nested keys?
Getting all the values of a hashtables nested keys?

Time:08-26

I have a Hashtable, called $Hash, This is its structure:

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 am trying to use the value of the Long key, (which is: "Procces is terminated, then all errors are dumped to a log file...") so I can use to input values to general things such as names for files:

$Var| ForEach-Object {
Copy-item -path '..\Template 2.svg' -Destination ".\($hash.keys.value).svg"
}

Doing something like $hash.'NTER 1'.keys is not an option, As there could be multiple $hash.<key> at any given time.

The expected output for the above example should be .svg files paths of:

.\Procces is terminated, then all errors are dumped to a log file1.svg
.\Procces is terminated, then all errors are dumped to a log file2.svg
...

Am having general problems just accessing data from hashtables for use in other commands.

For example in the above example. I could not do $Hash.keys | ForEach-Object { as that would not give me keys such as NTER 1 LA2 1 etc but some string of (System.Collections.Specialized.OrderedDictionary.keys.value) So I had to first create $var via $Var = $Hash

but even with this roundabout doing:

$Var| ForEach-Object {
Copy-item -path '..\Template 2.svg' -Destination ".\($hash.keys.value).svg"
}

Still gives me a single file called:

.\(System.Collections.Specialized.OrderedDictionary.keys.value).svg

I've taken the liberty to cross post this question on other forums.

Any help would be great.

CodePudding user response:

If you're looking to retrieve the values of the Long-keyed entries of the nested hashtables in $hash, across all top-level entries:

$hash.Values.Long
  • Related