flancy icon indicating copy to clipboard operation
flancy copied to clipboard

Pass variables to webschema's "script"

Open beatcracker opened this issue 9 years ago • 3 comments

We need some way to pass variables/objects to webschema's script.

Ideally, script should be passed and executed as an actual PowerShell scriptblock, with it's SessionState and so on. This way we could capture required variables with GetNewClosure or pass them to scriptblock as arguments.

Right now, if I understand it correctly, scriptblocks are used as text to build function that executed in nancy via PowerShell host:

string command = @"function RouteBody {{ param($Parameters, $Request) {0} }}";

beatcracker avatar Nov 01 '15 14:11 beatcracker

I'd love to be able to do this. I'm not sure I can though - I will be actively pursuing a solution over this week while I'm at the MVP summit in Redmond.

toenuff avatar Nov 01 '15 21:11 toenuff

I'm not sure that we can pass an actual PS scriptblock, but as viable workaround we can create a new InitialSessionState, create Runspace using it, assign this Runspace to a PowerShell object, and then import current variables/modules into it. Unfortunately, while it's all can be done in PowerShell, I have no idea how to pass resulting Runspace to the Flancy. Probably C# code has to be extended with some method/property that would accept Runspace object.

References:

beatcracker avatar Nov 01 '15 23:11 beatcracker

More thoughts on a subject:

  • If we could pass a runspace to a Nancy's PS host, we could ditch string command = @"function RouteBody {{ param($Parameters, $Request) {0} }}"; line entirely and instead add this function to a Runspace beforehand in PS part of module.
  • If this Runspace object could be passed as some kind of "shared" object (by reference?), we could modify it on the fly and script could be reloaded (but not routes, though) without restarting PowerShell.

beatcracker avatar Nov 03 '15 12:11 beatcracker