Question:
I am working with a config file for PowerShell and every time I call a foreach loop to grab the data it grabs it in the wrong order. See below:
config.ini
1 2 3 4 5 6 7 8 9 10 |
[queries] query01=stuff1 query02=stuff2 query03=stuff3 query04=stuff4 |
The foreach
loop:
1 2 3 4 5 6 7 8 9 10 |
#Loads an INI file data into a variable $iniContent = Get-IniContent 'config.ini' #grabs the hashtable for the data under the [queries] section $queries = $iniContent['queries'] foreach($query in $queries.GetEnumerator()) { write-host $query.name } |
I get the following output:
1 2 3 4 5 |
stuff1 stuff4 stuff2 stuff3 |
I am assuming this has something to do with asynchronous processing from PowerShell, but what would be the best way to handle the queries from the config.ini
file in the order I store them in there?
Note: I added the number to the end of the query (query01
) just for testing purposes. The queries will not have them in my final config.ini
.
EDIT:
Get-IniContent
function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
function Get-IniContent ($filePath) { $ini = @{} switch -regex -file $FilePath { “^\[(.+)\]” # Section { $section = $matches[1] $ini[$section] = @{} $CommentCount = 0 } “(.+?)\s*=(.*)” # Key { $name,$value = $matches[1..2] $ini[$section][$name] = $value } } return $ini } |
Answer:
You need to change both hashtable declarations to ordered dictionaries. If you only change
1 2 |
$ini = @{} |
to
1 2 |
$ini = [ordered]@{} |
your $ini is now an ordered dictionay but the nested hashtable created at
1 2 |
$ini[$section] = @{} |
is still an unordered hashtable. You would need to change both of them to ordered dictionaries.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
function Get-IniContent ($filePath) { $ini = [ordered]@{} switch -regex -file $FilePath { “^\[(.+)\]” # Section { $section = $matches[1] $ini[$section] = [ordered]@{} $CommentCount = 0 } “(.+?)\s*=(.*)” # Key { $name,$value = $matches[1..2] $ini[$section][$name] = $value } } return $ini } |
EDIT
There is also a ConvertTo-OrderedDictionary script on the Script Center that allows you to convert hashtables and arrays to ordered dictionaries if you don’t want to rewrite your function.