Declaring a scriptblock in a startup script fails when being called later on

Product: PowerShell Universal
Version: 4.1.8

I have a startup script for an environment that sets up a lot of functions to be used in APIs/Dashboards.

One of the functions accepts a scriptblock at a parameter and stores it in the $Cache: scope.

I call the function to store the scriptblock in $Cache at the end of my startup script.

When I invoke the script in my dashboard using “& $Cache:Script” the scriptblock fails.

When I call the function to store the scriptblock in $Cache in my dashboard, and invoke the script through the Cache scope the same way it works.

What could be causing this? I’ve tried everything and calling the function in the dashboard right before invoking it is the only way it works.

This sounds like a scoping issue. The Cache scope is not available through all environments.

Do you get the same results if you use the Server wide cache Get\Set-PSUCache?

As an aside, what is the reasoning to store script blocks this way? Kind of curious on the use case.

I’ve done a lot more troubleshooting,

I’m storing in $Cache: in the startup script for the environment the dashboard is running in, and I’m doing all of my troubleshooting through the session console, so it’s in the scope of the dashboard.

Calling the variable that has the script block returns the correct script. If I store the same scriptblock in a regular variable in the startup script:

$TestScriptBlock = { # Script Here }

It executes perfectly when calling it in the dashboard…

I then worked step by step turning $TestScriptBlock into what I had originally, this time storing it in $Global:TestScriptBlock, it works, then in an arraylist of objects with a scriptblock property, $Global:TestScripts[0].ImportScript, runs perfectly fine.

Eventually I get back to where I started, exact same code, but the only thing I had to change was the variable name, I originally had been using $Cache:Sources, but switched to $Global:Sources and had the same issue, so I switched again to $Global:TestSources and it worked.

I assumed it was some sort of variable clobber in the environment.

But then I called a function that runs each script block in the arraylist, and it broke, exact same error, even calling the script block directly now didn’t work.

It feels like I’m breaking the variables as I troubleshoot?? Im not sure what is happening anymore.

I undid all the changes I made back to a point where it was working, and restarted the entire VM hosting PSU, same error, with code that was just working.

Basically I’m pretty sure it’s not the $Cache scope since it’s also failing in the $Global scope, also since outputting the scriptblock always returns the correct script, just invoking it doesn’t work in specific situations.

I’m writing a user provisioning system, I have a New-Source function that defines a person source and stores a scriptblock that can be called later to import persons from an external system.

Can you share the bare bones of what you are storing during the startup script and then how you are running it? I can see if I can reproduce the behavior at least. I don’t currently have any code that does something like that.