New-UDTable I don't want to sort

Why it is getting sorted? I want the data as shown in the array. Code
CPD VM
CPD Phy
COC VM
COC Phy
I am using UD 2.3.2
The output windows getting sorted automatically Hope I am clear

i tested separately and it is same getting sorted. Here is the code
New-UDColumn -SmallSize 6 -Content {
New-UDTable -Title “Windows Team” -Headers @(‘Name’,‘Value’) -Endpoint {
@{
‘CPD VM’ = “10”
‘CPD Phy’=“20”
‘COC VM’=“100”
‘COC Phy’=“5”
}.GetEnumerator() | Out-UDTableData -Property @(“Name”, “Value”)
}
}

It looks like this is a powershell thing - even defining the exact hash table outside of UD will reset the order of your hash when you do a simple return…

There are probably a few solutions but you might have to massage the hash before pushing it up to the dashboard level.


Does it still sort it if you pass the hashtable as [ordered]?

[ordered]@{
‘CPD VM’ = “10”
‘CPD Phy’=“20”
‘COC VM’=“100”
‘COC Phy’=“5”
}.GetEnumerator() | Out-UDTableData -Property @(“Name”, “Value”)

artvandelay440, it gives an error when you paste in powershell
[ordered]@{
‘CPD VM’ = “10”
‘CPD Phy’=“20”
‘COC VM’=“100”
‘COC Phy’=“5”
}.GetEnumerator()

[ordered]@{
‘CPD VM’ = “10”
‘CPD Phy’=“20”
‘COC VM’=“100”
‘COC Phy’=“5”
} works well on PS window

if do this

[ordered]@{
‘CPD VM’ = “10”
‘CPD Phy’=“20”
‘COC VM’=“100”
‘COC Phy’=“5”
} | Out-UDTableData -Property @(“Name”, “Value”)
Comes blank in browser

My solution to that, is to add another property you can sort on, usually called “Sort”

$TableData =  @(
[PSCustomObject]@{Name = "Username";  Value = $($EmployeeAD.SamAccountName); Sort = "1"}
[PSCustomObject]@{Name = "Name";  Value = $($EmployeeAD.Name); Sort = "2"}
[PSCustomObject]@{Name = "Title";  Value = $($EmployeeAD.Title); Sort = "3"}
[PSCustomObject]@{Name = "Department";  Value = $($EmployeeAD.Department); Sort = "4"}
[PSCustomObject]@{Name = "Department Name";  Value = $($EmployeeAD.WWWHomePage); Sort = "5"}
[PSCustomObject]@{Name = "Account Expiration Date";  Value = $(if($EmployeeAD.AccountExpirationDate -eq $null) {"Never"} else {(Get-Date ($EmployeeAD.AccountExpirationDate)).tostring("yyyy-MM-dd HH-mm-ss")}); Sort = "6"}
 ).GetEnumerator() | Sort -property Sort
1 Like

so [ordered] is PSParser magic sauce. What it comes down to is that you can’t access (enumerate) over the properties of an object while trying to cast it ([ordered]) at the same time. 2 ways around it.

  1. Make it a subexpression:
([ordered]@{
    'CPD VM' = "10"
    'CPD Phy'="20"
    'COC VM'="100"
    'COC Phy'="5"
}).getenumerator() | Out-UDTableData -Property @("Name", "Value")
  1. Save it to a var then pipe it.
$table = [ordered]@{
    'CPD VM' = "10"
    'CPD Phy'="20"
    'COC VM'="100"
    'COC Phy'="5"
}

$table.getenumerator() | Out-UDTableData -Property @("Name", "Value")
2 Likes