Auto-generated Variable names in $Cache: Scope

Hello everyone,

My goal is to create and assign a value to a variable used for background color of a UD-Card’s -BackgroundColor parameter and store the variable to the $Cache scope. This work will be done in an endpoint and the result will need to be cached.

A list of server names is stored in $ServerList.

End Goal -
Have a variable named “$Cache:Server1background” with value of $BGpass
Have a variable named “$Cache:Server2background” with value of $BGpass
Have a variable named “$Cache:Server3background” with value of $BGpass

Attempts listed below -

foreach ($Server in $ServerList) {
        New-Variable -Name "$($server.name)background" -Value $Cache:BGpass
        New-Variable -Name "Cache:$($server.name)background" -Value $Cache:BGpass
        Set-Variable -Name "Cache:$($server.name)background" -Value $Cache:BGpass -Scope Global
        
    }

Any advices on this topic would be greatly appreciated. I’m hitting a wall on this one.

A quick test with my server names raises the issue of illegal characters in variable names. My resulting variable from "New-Variable -Name “$($server.name)background” -Value Cache:BGpass" is {asdf-asdfbackground}. The curly braces are added by powershell automatically.

Valid characters are only letters, numbers and underscore.

gav

The cache scope is actually just a PSProvider. It’s a neat trick but Set-Variable\New-Variable won’t work. Try New-Item or Set-Item.

PS C:\Users\adamr> Set-Item -PSPath "Cache:Test" -Value "test123"
PS C:\Users\adamr> $Cache:Test
test123

Cunning, I was wondering how that worked. How do we recall those variables in the context of this question, eg

foreach ($Server in $ServerList) {
New-UDCard -title $cache:$($server.name)background
}

isn’t going to work.

gav

You can use Get-Item.

PS C:\Users\adamr> $Cache:Test = "test123"
PS C:\Users\adamr> Get-Item "Cache:Test"
test123

Get-Item doesn’t work with variable variable names (to coin a phrase). At least not for me!
eg

$BGpass  = "asdf"
$serverlist = Get-AdComputer -filter * -seacrhBase "OU=Servers,OU=Department,DC=Company,DC=COM"
foreach ($Server in $ServerList) {
    Set-Item -PSPath cache:$($server.name)background -Value $BGpass      # works
    $title = Get-Item -PSPath cache:$($server.name)background            # doesn't work
    New-UDCard -Title $title
}

That’s really weird. Works fine for me.

PS C:\Users\adamr> Get-Item -PSPath "cache:$Test"
123

What version are you running?

Interesting. It works with a self contained scenario as above in a test page. It’s not working with cache variables set in a scheduled endpoint on my live dashboard.
I can call the cache:vars explicitly in debug but not be assembling the variable name from an array.
I’ll experiment.

gav (2.6.2)

By golly, Set-Item worked like a dream. It allowed me to collect all needed data. I’ve included a full script to show a working model.

I’m currently running the 10/8/19 nightly release.

$db = new-UDDashboard -Title "Dashboard 2.0" -Content {

    New-udrow -Columns {
        New-UDColumn -size 3 -Endpoint {
            New-UDCard -id "Test" -Title $Cache:Server1fullname -BackgroundColor $Cache:Server1background -Endpoint {
                
            }
        } -AutoRefresh -RefreshInterval 2
    }
}

$Cache:BGpass = [UniversalDashboard.Models.DashboardColor]"Green"


$Schedule_Server_Name = New-UDEndpointSchedule -Every 1 -second


$Endpoint_Server_Name = New-UDEndpoint -Schedule $Schedule_Server_Name -Endpoint {
    
    $Masterlist = Import-Csv "This is a CSV list of Server1, Server2, Server3"

    foreach ($server in $Masterlist) {

        Set-Item -PSPath "Cache:$($server.name)fullname" -Value $server.name
        Set-Item -PSPath "Cache:$($server.name)background" -Value $cache:BGpass
    }
}

Get-UDDashboard | Stop-UDDashboard
Start-UDDashboard -Dashboard $db -Port 10000 -Endpoint $Endpoint_Server_Name -AutoReload

Good morning,

Did you ever find a solution for this? I am attempting to loop through REVEAL cards to dynamically name them.

Having an issue with the syntax of Get-Item.