PowerShell Memo

このサイトはPowerShell(MSH/Monad)奮闘記です

管理人「newpops吉岡洋」が
「PowerShell(旧名:MSH/Monad)」の研究結果を日々綴っていきます。

【お知らせ】
この日記からPowerShellのTipsを抽出し「PowerShell FAQ」として整理しました。


2006-01-10

[][]Use of IronPython in Monad, Part 2

We can call IronPython in Monad.

See MSH Memo - Use of IronPython in Monad

Additionally, we can use the function of IronPython script in Monad.

Use the function of IronPython script in Monad

1.

Load IronPython.dll.

Create ScriptEngine Object of IronPython by new-object Cmdlet.

ex)

[void] [Reflection.Assembly]::LoadFrom("C:\IronPython\IronPython.dll")

$pe = new-object ironpython.hosting.pythonengine

2.

Call Execute method, Arg is the string that contains functions.

The following example is that $code contains "printStr" function.

ex)

$code=@"

def printStr(str):

print str

"@

$pe.Execute($code)

3.

Get the reference of function by GetVariable method.

ex)

$pPrintStr = $pe.GetVariable("printStr")

4.

Exectute function by Call method.

ex)

$pPrintStr.Call("Hello Monad!")

Sample Script
[void] [Reflection.Assembly]::LoadFrom("C:\IronPython\IronPython.dll")
$pe = new-object ironpython.hosting.pythonengine

$code=@"
def add(x, y):
    return x + y
"@

$pe.Execute($code)

$pAdd = $pe.GetVariable("add")

$pAdd.Call(5,20)
Sample Result
MSH C:\> [void] [Reflection.Assembly]::LoadFrom("C:\IronPython\IronPython.dll")
MSH C:\>
MSH C:\> $pe = new-object ironpython.hosting.pythonengine
MSH C:\>
MSH C:\> $code=@"
>> def add(x, y):
>>     return x + y
>> "@
>>
MSH C:\> $pe.Execute($code)
MSH C:\>
MSH C:\> $pAdd = $pe.GetVariable("add")
MSH C:\>
MSH C:\> $pAdd.Call(5,20)
25