Can't modify $Cache:var within a function

This is my first post in this forum (Greetings to all :-))
I am a beginner with universal dashboard community and I am stuck with the following code.

What I want to do is define a variable of type $ Cache: var and use it as a parameter so that its value is modified within a function, but I don’t get it.

I leave the code in case you can help me in this.

What happens is that the variable Cache: state_srv retains the original value of false when it is declared and I want its value to be modified when I pass it as a parameter.

Thank you

import-module -Name UniversalDashboard.Community

function Check_ping($srv)
    # We suppose the function retunr $true
    return $true

function check_srv
        [Parameter(Mandatory=$true, Position = 0)]
        [Parameter(Mandatory=$true, Position = 1)]

    $esta.value = Check_ping ($servidor)

$Cache:estado_srv = $false

$Schedule = New-UDEndpointSchedule -Every 30 -Second

$Every30sec = New-UDEndpoint -Schedule $Schedule -Endpoint {
    check_srv ("server1") ([ref]$Cache:estado_srv)

Start-UdDashboard -Content {
    New-UDDashboard -Title "Test" -Content {
        New-UDRow -Endpoint {         
	    # Here, $Cache:estado_srv has $false value... Wait?   
            New-UDCard -Title "Estado" -Text  "Global:estado_srv: $Cache:estado_srv" 
        } -AutoRefresh -RefreshInterval 10

}  -Endpoint $Every30sec -Port 10001 -AutoReload

Hello @paquechu and a very warm welcome to the UD forums…I believe you will need to use the $session variable which can change, so like if you had a drop down list you could hold what is selected in the $Session variable then have a function act on the $session value it is currently holding.
More info here:-

@psDevUK $Session variables don’t work in scheduled endpoints since they aren’t tied to a user.

It might be a problem with using the [ref].

I would suggest something like:

$myVar = $null 
check_srv ("server1") ([ref]$myVar)
$Cache:estado_srv = $myVar

The $Cache and $Session scopes are kinda weird since they are true PS scopes but rather drive providers that seem like variable scopes so sometimes stuff like this doesn’t work.


Hello @psDevUK , Thank you very much for the welcome and for the quick response :slight_smile:

Hello @adam, Thank you very much, I’m going to try it as soon as possible.

Hi adam,
Your solution works fine :slightly_smiling_face:
Thank You very much.

1 Like