[IronPython] Embedding Powershell and Write-Host

Stephen Ng stephen at theleengs.com
Wed Jan 14 00:58:27 CET 2009


I'm using the following code to embed Powershell into IronPython.  I hope to
eventually replace almost all of my (large) Powershell script with
IronPython.

  from System.Management.Automation.Runspaces import RunspaceFactory
  the_runspace = RunspaceFactory.CreateRunspace()
  the_runspace.Open()
  ri = RunspaceInvoke(the_runspace)
  out = ri.Invoke('. .\myscript.ps1')

I'm able to pass variables back and forth using
the_runspace.SessionStateProxy.Get/SetVariable, which is really cool.

My problem is that any calls to Write-Host fail with "Cannot invoke this
function becasue the current host does not implement it."  Of course I can
remove all the Write-Host's, but I really depend on them for status
information about how my script is running, and not having access to console
output makes my script much harder to debug.

This post: http://decav.co<http://decav.com/blogs/andre/archive/2008/05/01/invoking-powershell-scripts-from-an-application.aspx>
m/blogs/andre/a<http://decav.com/blogs/andre/archive/2008/05/01/invoking-powershell-scripts-from-an-application.aspx>
rchive/2008/05/<http://decav.com/blogs/andre/archive/2008/05/01/invoking-powershell-scripts-from-an-application.aspx>
01/invoking-pow<http://decav.com/blogs/andre/archive/2008/05/01/invoking-powershell-scripts-from-an-application.aspx>
ershell-scripts<http://decav.com/blogs/andre/archive/2008/05/01/invoking-powershell-scripts-from-an-application.aspx>
-from-an-applic<http://decav.com/blogs/andre/archive/2008/05/01/invoking-powershell-scripts-from-an-application.aspx>
ation.aspx<http://decav.com/blogs/andre/archive/2008/05/01/invoking-powershell-scripts-from-an-application.aspx>
says
"To get around this, you can remove *write-host* from your scripts and just
have them write single lines, or you can implement a simple *PSHost* (although
this is a bit harder than it sounds)."

What does "just have them write single lines mean?"  Any pointers on how I
can get this working under IronPython?

Thanks,

Steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20090113/32fabd77/attachment.html>


More information about the Ironpython-users mailing list