In a .NET application hosting a PowerShell runspace, is it possible to export host application functions to that runspace so that I have, say, PowerShell cmdlets that cause effects on my UI?
My goal is simply to be able to report progress messages back to my host application UI from a long-running PowerShell script (since by hosting PowerShell I have no console window.) I'm imagining something like:
C#:
// Speculative pseudocode:
class ReportProgressCmdlet : Cmdlet { ... } // Reports a message on the UI.
...
myRunspace.Register(new ReportProgressCmdlet(myUI));
PowerShell:
Report-Progress "Starting step 1." # UI shows "Starting step 1."
...
Report-Progress "Step 1 failed."
Is this possible? If I'm going about it wrong, happy to hear different solutions (events?) I've read all of the following questions and many others like them, and I don't THINK they're what I'm looking for:
How to expose functionality to PowerShell from within your application
Powershell Call Assembly Delegate
How to programmatically add a PSCmdlet to a Powershell pipeline?