poshbotio / PoshBot

Powershell-based bot framework
MIT License
536 stars 108 forks source link

Parameter Fields of function New-PoshBotCardResponse does not keep original order if ordereddictionary is used instead of hashtable #175

Closed judyanndixon closed 4 years ago

judyanndixon commented 4 years ago

If New-PoshBotCardResponse is called with a System.Collections.Specialized.OrderedDictionary type instead of a System.Collections.Hashtable type, the order of the members of the OrderedDictionary is not maintained after the call.

Expected Behavior

$info = Get-ComputerInfo

if ($info) { $fields = [ordered]@{ Name = $info.LogonServer OS = $info.OSName Uptime = $info.Uptime IPAddress = $info.IPAddress } New-PoshBotCardResponse -Type Normal -Fields $fields

fields order going into call Name Value


Name \DESKTOP-Q23N9QP OS Microsoft Windows 10 Pro Uptime IPAddress

fields order after the call (Notice the order of the Fields is not what was passed.) Type : Normal Text : Private : False DM : Fields : {Uptime, OS, IPAddress, Name} Color : #008000

What we like to see instead. (Notice the order of the Fields is what we passed in.) Type : Normal Text : Private : False DM : Fields : {Name, OS, Uptime, IPAddress} Color : #008000

Current Behavior

Not sure this behavior can be categorized as a bug or not. Current behavior is that as a result of the call the order of the fields in the Fields parameter is not maintained after the call as shown above.

Possible Solution

Because the Fields parameter is treated as a [hashtable] inside the New-PoshBotCardResponse function, order of the fields in the hashtable cannot be maintained. However, I was able to work around it locally by doing the following:

I replaced the [hashtable]$Fields line in the original source file with the following:

    [ValidateScript({
        $TypeName = $_ | Get-Member | Select-Object -ExpandProperty TypeName -Unique
        If ($TypeName -eq 'System.Collections.Hashtable' -or $TypeName -ne 'System.Collections.Specialized.OrderedDictionary') {
            [System.Collections.Hashtable]$_
        } ElseIf ($TypeName -eq 'System.Collections.Specialized.OrderedDictionary') {
            [System.Collections.Specialized.OrderedDictionary]$_
        }
    })]
    $Fields,

Steps to Reproduce (for bugs)

1.Create an instance of an OrderedDictionary type with an ordered list of fields 2.Make a call to New-PoshBotCardResponse using the Fields parameter and the instance of the OrderedDictionary from above. 3.After the call make note of the order of the fields in the original OrderedDictionary sent in 4.The order will not be maintained after the call.

Context

We are trying to create cards using the Fields parameter that will maintain the order of the fields in the OrderedDictionary we send in to the call.

Your Environment

devblackops commented 4 years ago

Thanks @judyanndixon for the detailed report and possible solution! That helps a bunch. I'll dig into this. 👍

judyanndixon commented 4 years ago

Thanks for fixing this so fast.