Forum Discussion
John_Dodo
Feb 10, 2022Brass Contributor
Ordering data in hash of hashes
Hello,
I have this piece of code :
...
#Get GPOs with all settings disabled
$GpoList |
Where-Object {$_.gpostatus -eq "allsettingsdisabled"} |
ForEach-Object{
$CandidateGPOs[$_.id] =
@{
ID = $_.id
DisplayName = $_.displayname
CreationTime = $_.creationtime
Modificationtime = $_.modificationtime
Owner = $_.Owner
Domain = $domain
All_Settings_Disabled = $CandidateGPOs[$_.id].All_Settings_Disabled +"True"
Not_linked = $CandidateGPOs[$_.id].Not_linked +""
Links_disabled = $CandidateGPOs[$_.id].Links_disabled +""
No_settings = $CandidateGPOs[$_.id].No_settings +""
}
}
...
#other similar loops checking other settings (not linked, links disabled, no settings).
$candidateGPOs.GetEnumerator() | Sort-Object $_.keys | ForEach-Object{[PSCustomObject]$_.value } | Export-Csv "$ReportPath\_candidateGPOs_Listing.csv" -NoTypeInformation -Append -delimiter ";"
The result is not in the same order in the CSV.
The command give this order : $candidateGPOs.GetEnumerator() | Sort-Object $_.keys | ForEach-Object{[PSCustomObject]$_.value } =>
Modificationtime : 13.06.2019 11:24:02
ID : b17dfccad9c-a93c0-41a9-8d04-e119a549450f
CreationTime : 24.03.2007 14:27:45
No_settings : True
Domain : contoso.comt
Not_linked :
Owner : contoso\Domain Admins
All_Settings_Disabled :
Links_disabled :
DisplayName : Default Domain Controllers Policy
It was working before. I don't understand what I wrongly changed...
Thank you
O
- dotpwshCopper ContributorTake a look at ordered hashtables: https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-hashtable?view=powershell-7.2#ordered-hashtables
- John_DodoBrass ContributorActually it works. Thank you