How can I use colored text in New-UDTable for UD V3

Hey everyone,

I’m relatively new coming into UD, and I’m starting with UD V3. I have found several examples on using colored text in a UD V2 Table, but I can’t seem to get this to work in UD V3. Is it possible, yet? If so, can someone point me to a working example, or take a shot at troubleshooting my code?

I uninstalled the previous version and deleted C:\ProgramData\PowerShellUniversal, prior to installing the latest version 1.4.6. I’m running this on Server 2019, and installed using the WMI file.

I’m starting with something simple, like below, and once I can get the colored text working I’ll expand with other DC tests and plug them into a DC Health table.

Using the below code, the table says [object Object] as the values of the “Online Status” column. If I change the if/else to simple string values of “True” or “False”, they are properly displayed in the table. How can I get colored text for these column values in the table?

When running the code in VS Code, line by line, everything works without error until the final line of New-UDDashboard.

The shell gives the error:
MethodException: C:\ProgramData\PowerShellUniversal\Dashboard\Frameworks\UniversalDashboard\3.1.4\UniversalDashboard.MaterialUI.psm1:2387:5
Line |
2387 | $Content.Register($Id, $Role, $PSCmdlet)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Cannot find an overload for “Register” and the argument count: “3”.

When I browse to the dashboard and look at the log, I see the error:
PS: One or more errors occurred. (Cannot overwrite variable null because it is read-only or constant.) (Cannot overwrite variable null because it is read-only or constant.)

Thanks in advance!

$DCs = Get-ADDomainController -Filter * | Select-Object -first 5
$DCsHealth = @()

foreach ($DC in $DCs)
    {
    $pingTest = Test-Connection -Quiet -Count 1 $DC.HostName

    if ($pingTest) {$online = New-UDElement -Tag 'div' -Attributes @{style = @{'backgroundColor' = 'green'; 'color' = 'white'}} -Content {"True"}}
    else {$online = New-UDElement -Tag 'div' -Attributes @{style = @{ 'backgroundColor' = 'red'; 'color' = 'white'}} -Content {"False"}}

    $DCsHealth += [PSCustomObject][Ordered]@{
        'Name' = $DC.HostName
        'IP' = $DC.IPv4Address
        'Online' = $online
        }# $DCsHealth
    }# foreach ($DC in $DCs)

$columns = @(
    New-UDTableColumn -Property Name -Title "DC Name"
    New-UDTableColumn -Property IP -Title "IP Address"
    New-UDTableColumn -Property Online -Title "Online Status"
)# columns

$table_DCsHealth = New-UDTable -Id 'customColumnsTable' -Data $DCsHealth -Columns $columns -Title "Domain Controllers Online Status"

$grid = New-UDGrid -Container -Content {
    New-UDGrid -Item -LargeSize 6 -Content {$table_DCsHealth}
    }# $grid

New-UDDashboard -Title "Component Test" -Content {$grid}

Try converting your PSCustomObject to hashtable

When working on the table code, i also noticed this strange behavior, after i convert from object to hashtable it was working fine

Hey @AlonGvili,

Thanks for the suggestion, but I still have the same issue.

In the original code I posted, I converting the PSObject to a Hash Table, but no luck. I even changed it to create a hash table in the first place instead of a PSObject, and no luck.

I’ve taken this down to the most simple way I can think of. Instead of passing an array of PSObjects or hash tables, I’m creating a single PSObject or hash table, but I still have the same problem.

In the below code; I’ve tried using both New-UDTypography and New-UDElement. I’ve tried creating as a hash table, and as a PSObject, but still get the same results in the table of [object Object].

Thanks,

$hash = @{
    'DC Name' = "NameOfDC.domain.com"
    'IP Address' = "192.168.0.1"
    # 'Online Status' = New-UDTypography -Text 'True' -Style @{ color = 'green' }
    'Online Status' = New-UDElement -Tag 'div' -Attributes @{style = @{'backgroundColor' = 'green'; 'color' = 'white'}} -Content {"True"}
    }# $hash

$table = New-UDTable -Id 'Blam' -Data $hash -Title "Domain Controllers Online Status"

New-UDDashboard -Title "Blam Test" -Content {$table}

UDTable-objectObject

you need to used New-UDTableColumn
here is example

    $Data = Get-Process | Select-Object -Property ProcessName,WorkingSet,Id,VirtualMemorySize 
        $Columns = @(
            New-UDTableColumn -Property ProcessName -Title ProcessName -Render { 
                $Item = $EventData
                New-UDButton -Id "btn$($Item.ProcessName)" -Text $Item.ProcessName -OnClick { Show-UDToast -Message $Item.ProcessName } 
            } -Width 170
            New-UDTableColumn -Property WorkingSet -Title WorkingSet -Width 170
            New-UDTableColumn -Property Id -Title Id -Width 70
            New-UDTableColumn -Property VirtualMemorySize -Title VirtualMemorySize -Align right -Width 80
            New-UDTableColumn -Property Actions -Render { 
                # $Item = $EventData
                # New-UDButton -Text Update -OnClick { Show-UDToast -Message $Item.Id } 
            New-UDElement -Tag 'div' -Attributes @{style = @{'backgroundColor' = 'green'; 'color' = 'white'}} -Content {"True"}
            } 
        )

        New-UDTable -Data $Data -Columns $Columns -Title 'New UD Table Component' -PageSize 8

the results

Hey @AlonGvili,

Thank you so much for the example. I was able to get this working with only a single change to the original code.

I had to change the line: New-UDTableColumn -Property Online -Title “Online Status”
To: New-UDTableColumn -Property Online -Render {$EventData.Online}

To note, I tested this with creating and using a PSCustomObject and with a Hash Table. Works both ways for me!

Thanks again; I can now move forward with some stuff and things :slight_smile:

Update code that is working for me…

$DCs = Get-ADDomainController -Filter * | Select-Object -first 5
$DCsHealth = @()

foreach ($DC in $DCs)
    {
    $pingTest = Test-Connection -Quiet -Count 1 $DC.HostName

    if ($pingTest) {$online = New-UDElement -Tag 'div' -Attributes @{style = @{'backgroundColor' = 'green'; 'color' = 'white'}} -Content {"True"}}
    else {$online = New-UDElement -Tag 'div' -Attributes @{style = @{ 'backgroundColor' = 'red'; 'color' = 'white'}} -Content {"False"}}

    $DCsHealth += [PSCustomObject][Ordered]@{
        'Name' = $DC.HostName
        'IP' = $DC.IPv4Address
        'Online' = $online
        }# $DCsHealth
    }# foreach ($DC in $DCs)

$columns = @(
    New-UDTableColumn -Property Name -Title "DC Name"
    New-UDTableColumn -Property IP -Title "IP Address"
    New-UDTableColumn -Property Online -Render {$EventData.Online}
)# columns

$table_DCsHealth = New-UDTable -Id 'customColumnsTable' -Data $DCsHealth -Columns $columns -Title "Domain Controllers Online Status"

$grid = New-UDGrid -Container -Content {
    New-UDGrid -Item -LargeSize 6 -Content {$table_DCsHealth}
    }# $grid

New-UDDashboard -Title "Component Test" -Content {$grid}
1 Like