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.