Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I host PowerShell 3.0 in a c# app using a similar API to other DLR languages

I've been playing with a c# app that hosts IronPython, IronRuby, and (hopefully) PowerShell. Since IronPython and IronRuby were completely built on the DLR, the API for using them is pretty much identical.

IronPython.Hosting.Python.CreateEngine()

and

IronRuby.Ruby.CreateEngine()

both create instances of a Microsoft.Scripting.Hosting.ScriptEngine. Is there any hope of coercing PowerShell 3.0 to create a ScriptEngine? I have not been able to find much on the subject, other than PowerShell 3.0 seem be built on the DLR more than the previous version was (see http://huddledmasses.org/powershell-3-finally-on-the-dlr.

It doesn't appear that you can cast a PowerShell engine created with the following to a ScriptEngine.

System.Management.Automation.PowerShell.Create()

I suspect that if I really want to handle PowerShell through the same API I need to create my own ScriptEngine that wraps the PowerShell host.

like image 782
Brad Campbell Avatar asked Jul 22 '12 02:07

Brad Campbell


Video Answer


1 Answers

PowerShell V3 does indeed use some of the DLR, but only parts that shipped in the .Net Frameworks V4. The DLR project had many useful apis that were not included in .Net, e.g. Microsoft.Scripting.Hosting.ScriptEngine.

If PowerShell wanted to implement the ScriptEngine apis, PowerShell would have had to ship those apis as part of PowerShell. The PowerShell team wasn't willing to take ownership of those apis.

So you are correct in assuming you need to wrap PowerShell with your own code if you want to implement ScriptEngine.

like image 155
Jason Shirk Avatar answered Sep 21 '22 11:09

Jason Shirk