Sorting Tables in Universal Dashboard Framework

Is it possible to sort Tables within the PowerShell Universal Dashboard framework?

I cant for the life of me get it to work. See my code below:

Get-UDDashboard | Stop-UDDashboard
$LenelComDashboard = New-UDDashboard -Title “Lenel Communication Server Health” -Content {

New-UDTable -Title “Service Status” -Headers @(‘Hostname’, ‘CPU Usage’, ‘Service Status’) -Endpoint {
$serverlist = get-content -path C:\temp\Serverlist.csv
foreach ($servername in $serverlist){
Get-Service -ComputerName $servername -Name ‘LS Communication Server’ | Sort-Object -Property ‘Status’ | ForEach-Object {

        $BgColor = 'green'
        $FontColor = 'white'
        if ($_.Status -ne 'Running') {
            $BgColor = 'red'
            $FontColor = 'white'
        }

        [PSCustomObject]@{
            Name = $servername
            Status = New-UDElement -Tag 'div' -Attributes @{ style = @{ 'backgroundColor' = $BgColor; color = $fontColor } } -Content { $_.Status.ToString() }
        }
    } | Out-UDTableData -Property @("Name", "CPU Usage", "Status") 

}
}
}
Start-UDDashboard -Dashboard $LenelComDashboard -Port 8080 -AutoReload
#Get-UDDashboard | Stop-UDDashboard

Yes, I would sort the input object prior to feeding to Out-UDTableData.

Example of doing this with Out-UDGridData.

New-UdGrid -Title "Storage Array Capacities" -Headers @("DataCenter", "ORG", "Name", "Class", "Model", "Usage", "Capacity (TB)", "Used (TB)", "Free (TB)", "Full%", "Subs%") ` -Properties @("DataCenter", "Org", "Name", "Class", "Model", "usage", "total_usable_tb", "total_used_tb", "total_free_tb", "percent_full", "subs_percent") ` -AutoRefresh -RefreshInterval 360 -Endpoint { $byPercentFull = $PSArrayCap | Sort-Object -property {[int]$_.'percent_full'} -Descending; $byPercentFull | Select-Object DataCenter,Org,Name,Class,Model,usage,total_usable_tb,total_used_tb,total_free_tb,percent_full,subs_percent | Out-UDGridData }

Add a numeric property to the data and do the sorting on that.

New-UDTable -Id "OverviewTable" -Headers @("Types", "Count")  -Endpoint {
                        @(
                            [PSCustomObject]@{Sort = "00"; Value = $(($Cache:AllStaff).count);                                                              Name = "All Staff";      Link = New-UDLink -Text "All Staff" -Url "/EmployeeTypes/AllStaff"}
                            [PSCustomObject]@{Sort = "01"; Value = $(($Cache:AllStaff | Where-Object -Property EmployeeType -eq 'BlueCollar').count);       Name = "BlueCollar"; Link = New-UDLink -Text "All Employees" -Url "/EmployeeTypes/AllEmployees"}
                            [PSCustomObject]@{Sort = "02" ;Value = $(($Cache:AllStaff | Where-Object -Property EmployeeType -eq 'Fixed-Term').count);       Name = "Fixed-Term"; Link = New-UDLink -Text "Fixed Term" -Url "/EmployeeTypes/FixedTerm"}
                            [PSCustomObject]@{Sort = "03" ;Value = $(($Cache:AllStaff | Where-Object -Property EmployeeType -eq 'Senior').count);           Name = "Senior"; Link = New-UDLink -Text "Seniors" -Url "/EmployeeTypes/Seniors"}
                            [PSCustomObject]@{Sort = "04" ;Value = $(($Cache:AllStaff | Where-Object -Property EmployeeType -eq 'Student-Worker').count);   Name = "Student-Worker"; Link = New-UDLink -Text "Student Workers" -Url "/EmployeeTypes/StudentWorkers"}
                            [PSCustomObject]@{Sort = "05" ;Value = $(($Cache:AllStaff | Where-Object -Property EmployeeType -eq 'Intern').count);           Name = "Interns"; Link = New-UDLink -Text "Interns" -Url "/EmployeeTypes/Interns"}
                            [PSCustomObject]@{Sort = "06" ;Value = $(($Cache:AllStaff | Where-Object -Property EmployeeType -eq 'Trainee').count);          Name = "Trainees"; Link = New-UDLink -Text "Trainees" -Url "/EmployeeTypes/Trainees"}
                            [PSCustomObject]@{Sort = "07" ;Value = $(($Cache:AllBlueCollarWorkers).count);                                                  Name = "Blue Collars"; Link = New-UDLink -Text "Blue Collars" -Url "/BlueCollars"}
                            [PSCustomObject]@{Sort = "08" ;Value = $(($Cache:AllStaff | Where-Object -Property EmployeeType -eq 'Director').count);         Name = "Director"; Link = New-UDLink -Text "Directors" -Url "/EmployeeTypes/Directors"}
                            [PSCustomObject]@{Sort = "09" ;Value = $(($Cache:DeptManagers).count);                                                          Name = "Department Managers"; Link = New-UDLink -Text "Department Managers" -Url "/Departments"}
                            [PSCustomObject]@{Sort = "10" ;Value = $(($Cache:AllStaff | Where-Object -Property EmployeeType -eq 'Expat').count);            Name = "Expats"; Link = New-UDLink -Text "Expats" -Url "/EmployeeTypes/Expats"}
                            [PSCustomObject]@{Sort = "11" ;Value = $(($Cache:AllStaff | Where-Object -Property EmployeeType -eq 'Team-Leader').count);       Name = "Team Leaders"; Link = New-UDLink -Text "Team Leaders" -Url "/EmployeeTypes/TeamLeaders"}
                            [PSCustomObject]@{Sort = "12" ;Value = $(($Cache:AllConsultants).count);                                                        Name = "Consultants"; Link = New-UDLink -Text "Consultants" -Url "/Consultants-Dashboard"}
            
    
                        ).GetEnumerator() | Sort-Object Sort | Out-UDTableData -Property @("Link", "Value")

In this sample, I have added a Property called Sort, added ascending numbers in the order I want, and simply sort that.

1 Like

Thank you for both of your suggestions but what I’m trying to achieve is to sort the table so that services that are not running appear at the top so when the table is refreshed.

@Neer94 I think you would be better off using a grid instead of a table. Then the grid has automatic sorting and filtering. Look at poshud.com for an example. Peace