Is there a way to pass variables to endpoints?

I’m not new to software development, but I’ve only been working with PowerShell for a few months, I’m learning as I go, which is painful in a fun kind of way. I’ve run in to this weird issue with UD where global variables aren’t global in Endpoints because Endpoints are hosted as part of an isolated runspace pool and not in the same execution environment as the PowerShell session that started the dashboard. They simply cease to exist.

That’s cool, I appreciate and respect the design decision that led to this, even though I think it’s a bit cruel to the unsuspecting developer and the innocent data that gets chucked under a bus every time an endpoint is instantiated.

My question is; Rather than having to re-define all the global variables inside every endpoint, is there a way to pass them in like you would with a function? This would avoid code duplication, mystery bugs and a massive headache if I need to refactor.

The cache variable is avaliable to all the endpoints.
See:
https://docs.universaldashboard.io/endpoints/variables-defined-in-endpoints

IE:
$cache:logfile = “c:\locationtologfile\logfile.txt”
The variable should be avaliable to use in all endpoints.

1 Like

Thank you. Perhaps it would be worth adding a note about this in the documentation…

https://docs.universaldashboard.io/concepts

Found the correct link:
https://docs.universaldashboard.io/endpoints/custom-variable-scopes
Its under endpoints :slight_smile:

1 Like

Hello @robert.heritage thanks for your question. As @BoSen29 kindly mentioned the sacred $Cache variable, there is another trick you can use.
https://docs.universaldashboard.io/endpoints/endpoint-initialization
So I like to split all my pages as separate pages, so I can save the variable $PSScriptRoot as a $Root variable which I can the reference $Root variable anywhere in my dashboard script and it will know what it is. The real benefit to this is using functions. AS instead of having to place the whole function in an endpoint if you define it as an Endpoint Initialization you can just “call” that function throught your whole dashboard, instead of having to paste the entire function in every endpoint you want to call it in. I have an example of both these methods in a dashboard project I uploaded on github to help others:- https://github.com/psDevUK/psUniversalDashboard/blob/master/FleetManagementGitHub.ps1 hopefully with this and the help @BoSen29 provided this should answer your question? :smile:

2 Likes