Colleague Mike Davey was talking about hosting a scripting engine in his WPF application. He is at a choice point and is considering LUA, PowerShell, IronPython or IronRuby.
One question he has is how to pass part of your object model into PowerShell so you can manipulate it with script.
This short screen cast shows how to host the PowerShell scripting engine, create a variable and set it to a WPF visual control. You can use this approach to set variables to instances of your object model as well.





{ 7 trackbacks }
{ 8 comments… read them below or add one }
Thanks, Doug. This was a lot of help.
Nice demo.
While your example works fine, the InitialSessionState api was designed for this scenario. It will create the variables as part of the runspace creation rather than using SessionStateProxy to add variables after the runspace is opened.
Thanks Jason, great info I will re-work the code.
Nice demo. Probably about as short as possible, and still allows you to see the power in the approach. I would definitely vote for powershell as a scripting language. I have built a custom host that has some custom cmdlets to interact with the environment, and it makes customization really easy. I’m using 1.0 instead of 2.0 (since 2.0 isn’t final it’s hard to get support from mgmt), so I have to jump through some hoops to affect the controls, but the end result is a lot of fun.
Great video. Exactly what I was looking for. Thanks for sharing this.
Nice one. Some powershell cmdlets need user input (prompts). is there any way to insert them?
Thanks for the comment Rajeevan. The answer is yes. I don’t know how to do it. The PowerShell Integrated Scripting Environment (ISE) does just that. My guess to handling that is an override when building the custom host.
I’ll look into it and will post a response when I have a solution.
Post a question to StackOverflow and tweet it.
Doug
How can I import an initial module?
Runspace rs = RunspaceFactory.CreateRunspace();
rs.ThreadOptions = PSThreadOptions.UseCurrentThread;
rs.Open();
// How can i import a default module to the runspace?
// rs.InitialPSModule = .ImportPSModule(new[] { @”C:\Intellitrace\Microsoft.VisualStudio.IntelliTrace.PowerShell.dll” });
Powershell ps = Powershell.Create();
ps.Runspace = rs;
ps.AddScript(@”Start-IntelliTraceCollection ‘DefaultAppPool’ C:\Intellitrace\collection_plan.ASP.NET.trace.xml C:\IntellitraceLogs”);
ps.Invoke();
rs.Close();