I have a script with an Textbox, a Button and a table.
When I press the button, some data gets retrieved and the table populated.
This works fine.
However when I press the button again, the same variable is empty and I get an error.
When I press the button again, all is fine.
Next click: error. And so on.
I can’t share the original script, so I have created something to show the problem.
Edit: updated the script and updated psu-server to 4.3.3
You say “some variable”… Is that to imply that it’s randomly selecting which variable is going to be empty? Or, is it the same variable each time that is empty on every other click?
sorry, the second “some” should have been “the same”.
When you look at the code, I define a $session:out variable.
One 1st click the variable is accessible, contains data and can be updated.
One 2nd click the variable is empty.
One 3rd click the variable is usable again.
Next, empty and so on.
I have that issue in more then one script.
That is because I found a way (just started with PSU) to create somewhat dynamic output from either psremote session calls or API calls using scriptblocks (instead of functions).
I was quite happy with the outcome, pretty simple web GUI for stuff I already have.
And then … this.
I tried the -wait option on the sync-udelement.
I added sleep commands…
I tried page and session variables.
I kept data amount in those variables low. (anything bigger I store in a file)
I already thought of keeping my variables in a json file…
But I think this should work anyway.
No. It just made this example work.
My other scripts still show the old behavior, even after several changes.
They are more complex and do more things…But the issue is similar, every second round some variables are not accessible.
Right now I am working on one where I can’t even set variable in a scriptblock.
On the first run, scriptblocks “know” script variables.
On the second run, initiated by button click, they don’t and there will be an error.
On the third try, behavior is like on first run, and so on.
So never expect any variable, any scope to “just” be in a scriptblock. (well, seems cache scope works…)
Show-udtoast and set-udelement clearly are no-no in script blocks - if you want to call them more then one time.
The reason is that the script block captures the closure of the location you are defining it in. The problem is that we reconstruct script blocks in apps like this, so the closure is no longer valid and probably in some weird state. No idea why it works once but using GetNewClosure() captures the current state and then should work.