Transfer value from javascript to dashboard

Hello,
I am using Invoke-UDJavaScript -Javascript and it is working well.
I want to use the result from javascript in Dashboard. How can I pass the value of mylist from javascript back to dashboad?

New-UDButton -Id ‘test’ -Text ‘test’ -OnClick {
Invoke-UDJavaScript -Javascript “test();”
}

the javascript file:
function test()
{

var mylist = result_array
}

any help is highly appreciated.

I really need to have a solution to my prolem above.
Please kindly help.

There currently isn’t a way to transfer a value from JS back to PS. You would likely need to call an API endpoint from JavaScript and then invoke some PowerShell to store that data and then read it back in the dashboard.

Many thanks Adam ! i am actually a beginner. Would you please have a small example, just a view lines, for me? That would help me a lot.

I would appreciate an example for how to call API (POST?) from Javascript and how to get it from a dashboard.

I’m working on an example. Should have one soon.

In your dashboard, you can setup this:

New-UDDashboard -Title "Hello, World!" -Content {
    New-UDButton -Text "Send From JavaScript" -OnClick {
        Invoke-UDJavaScript "fetch('/object', { method: 'POST', body: 'hello from js!'})"        
    }

    New-UDButton -Text 'Get From Cache' -OnClick {
        $Value = Get-PSUCache -Key 'FromJavaScript'
        Show-UDToast $Value
    }
}

In my /object endpoint, I’m using the cache.

Set-PSUCache -Key 'FromJavaScript' -Value $Body

So the I can click the first button to set the item from javascript and click the second button to read it.

1 Like

Thank you very much Adam! It works!.

1 Like