[IronPython] Embedding Powershell and Write-Host

Stephen Ng stephen at theleengs.com
Fri Jan 16 17:04:57 CET 2009


My VC .NET is apparently too old to do this :-(.
But then I realized that I could get close enough by:

1. moving the main loop of my program from Powershell to Python

2. Rewriting Write-Host to stuff strings into a global:


$global_log_messages = ''
function Write-Host($s) {
  Set-Variable -Name 'global_log_messages' -Value ($global_log_messages + `
    "`n" + $s) -Scope global
}


3. Periodically dumping the log messages from my IronPython code:

VAR_LOG_MESSAGES = 'global_log_messages'

def PrintLogMessages(session_state_proxy, logger=sys.stdout):
  logger.write(session_state_proxy.GetVariable(VAR_LOG_MESSAGES) + '\n')
  session_state_proxy.SetVariable(VAR_LOG_MESSAGES, '')


Let me know if you see a better way....

Steve

On Tue, Jan 13, 2009 at 5:35 PM, Curt Hagenlocher <curt at hagenlocher.org>wrote:

> Looking at the definitions of the C# classes in that blog entry and given
> that you're hosting PowerShell inside of IronPython, I see no reason why you
> wouldn't be able to implement the derived types in Python itself.  But the
> link seems to do a pretty good job of giving minimal implementations for
> both classes.
>
>
> On Tue, Jan 13, 2009 at 5:11 PM, Stephen Ng <stephen at theleengs.com> wrote:
>
>> Ouch, I guess I have to write some C#!
>> There's no way to do this all in IronPython?
>>
>> Steve
>>
>>
>> On Tue, Jan 13, 2009 at 4:06 PM, Curt Hagenlocher <curt at hagenlocher.org>wrote:
>>
>>> You might find this link helpful.  It was the second hit when googling
>>> "implement pshost", and the author talks about integration between IronRuby
>>> and PowerShell.
>>>
>>> http://bartdesmet.net/blogs/bart/archive/2008/07/06/windows-powershell-through-ironruby-writing-a-custom-pshost.aspx
>>>
>>> On Tue, Jan 13, 2009 at 3:58 PM, Stephen Ng <stephen at theleengs.com>wrote:
>>>
>>>> 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
>>>>
>>>> _______________________________________________
>>>> Users mailing list
>>>> Users at lists.ironpython.com
>>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>>>
>>>>
>>>
>>> _______________________________________________
>>> Users mailing list
>>> Users at lists.ironpython.com
>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>>
>>>
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20090116/b4fd65d0/attachment.html>


More information about the Ironpython-users mailing list