3

I'm trying to collect some info from remote hosts in form of a CSV string. I need the values to be returned in particular order to accumulate them in one CSV file.

I use ordered hash table to keep keys in particular order, but when I pass it to a remote host the order gets broken.

Is there a way to preserve the order?

$ordered_hash = [ordered]@{"a" = 1; "b" = 2; "c" = 3}
$ordered_hash
$scriptblock = {
param ($hash)
$hash
} 
Invoke-Command -ComputerName localhost -ScriptBlock $scriptblock -ArgumentList ([hashtable]$ordered_hash)

Returns:

Name                           Value                                                                                                                                     
----                           -----                                                                                                                                     
a                              1                                                                                                                                         
b                              2                                                                                                                                         
c                              3                                                                                                                                         
c                              3                                                                                                                                         
b                              2                                                                                                                                         
a                              1                                                                                                                                         
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
Papa Smurf
  • 41
  • 3

1 Answers1

2

When you define a hash table using [ordered] attribute, in fact it creates a System.Collections.Specialized.OrderedDictionary.

When passing it to a parameter of HashTable type, it converts the OrderedDictionary to HashTable which is no more ordered.

To keep the order, you can use either of following options:

  • Remove the type from parameters of the script block to prevent a type change. This way it will use the original type which is use OrderedDictionary.

Or

  • Use System.Collections.Specialized.OrderedDictionary type of the parameter of the script block.
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 1
    I tried both options with the example which you shared and both of them work properly. Let me know if you have any question about the answer ... – Reza Aghaei Dec 18 '17 at 06:09
  • Didn't work for me. I got the same result. $ordered_hash = [ordered]@{"a" = 1; "b" = 2; "c" = 3} $ordered_hash $scriptblock = { param ($hash) $hash } Invoke-Command -ComputerName localhost -ScriptBlock $scriptblock -ArgumentList ([System.Collections.Specialized.OrderedDictionary]$ordered_hash) – Papa Smurf Dec 18 '17 at 11:58