Changing Background Color of a UDCard from a scheduled Endpoint

$db = New-UDDashboard -Title "Blank Dashboard" -Content {
    New-UDCard -Id "Card1" -Title "Title" -BackgroundColor Yellow -Endpoint {

    }
}

$schedule = New-UDEndpointSchedule -Every 5 -Second
$Endpoint = New-UDEndpoint -Schedule $schedule -Endpoint {
    
    $bgpass = [UniversalDashboard.Models.DashboardColor]"Green"
    $bgfail = [UniversalDashboard.Models.DashboardColor]"Red"
    $bgother = [UniversalDashboard.Models.DashboardColor]"Yellow"
    $fontColor = [UniversalDashboard.Models.DashboardColor]"Black"

    $bgcolors = @($bgpass,$bgfail,$bgother)
    
    $bgcolor = $bgcolors | Get-Random

    $attributes = Get-UDElement -Id "Card1" | select -exp Attributes
    $attributes.style.backgroundColor = $bgcolor
    $attributes.style.Color = $fontColor

    Set-UDElement -Id "Card1" -Attributes $attributes
    Sync-UDElement -Id "Card1"
}
Start-UDDashboard -Dashboard $db -Endpoint $Endpoint -AutoReload 

Trying to get this code to change the background color of the card randomly every 5 seconds, and it’s not working. Any help would be appreciated. I’m on version 2.3.2 pulled from PoshGallery running Windows 10, Powershell 5.1, and Chrome 72.

1 Like

A scheduled endpoint isn’t linked to a particular browser session so you can use Get\Set\Sync-UDElement from there. (on a bit of a tangent there is a -Broadcast parameter that should work but it looks like there is a bug there). So what you could do instead is set a $Cache variable and then setup the UI to autorefresh every 5 seconds. Here’s an updated example:

$db = New-UDDashboard -Title "Blank Dashboard" -Content {

    New-UDRow -Columns {
        New-UDColumn -Size 12 -Endpoint {
            New-UDCard -Id "Card1" -Title "Title" -BackgroundColor $Cache:Card1BackgroundColor -FontColor $Cache:Card1Color -Endpoint {

            } 
        } -AutoRefresh -RefreshInterval 5
    }

    
}

$schedule = New-UDEndpointSchedule -Every 5 -Second
$Endpoint = New-UDEndpoint -Schedule $schedule -Endpoint {
    
    $bgpass = [UniversalDashboard.Models.DashboardColor]"Green"
    $bgfail = [UniversalDashboard.Models.DashboardColor]"Red"
    $bgother = [UniversalDashboard.Models.DashboardColor]"Yellow"
    $fontColor = [UniversalDashboard.Models.DashboardColor]"Black"

    $bgcolors = @($bgpass,$bgfail,$bgother)
    
    $bgcolor = $bgcolors | Get-Random

    $Cache:Card1BackgroundColor = $bgcolor 
    $Cache:Card1Color = $fontColor
}
Start-UDDashboard -Dashboard $db -Endpoint $Endpoint -AutoReload
1 Like

Ok, I think this gives me what I need. Appreciate the help!