Tuesday, June 26th, 2012

IronPython and Microsoft's PowerShell

Ever find the need to execute some Python code from with Microsoft's PowerShell? Look no further than this getting started example. You will need to install both PowerShell 2.0 and IronPython. You will need to update the version you are using in the example below, in this example I am using version 2.7.1, which works perfectly.

[reflection.assembly]::LoadFrom("C:\Program Files (x86)\IronPython 2.7.1\IronPython.dll")
$py = [ironpython.hosting.python]::CreateEngine()
$py.Execute("print 'IronPython Engine loaded.'")

This very brief example will confirm that the engine has been loaded and works, by printing a simple message using Python onto your console. I have this in my $profile.

Our next example will use some of these wonderful examples from Chris Umbel's article Scripting Your .Net Applications with IronPython. In his examples, he explains how to add scripting to traditional C# applications, which is mighty helpful. I used his examples to build something similar using PowerShell:

[reflection.assembly]::LoadFrom("C:\Program Files (x86)\IronPython 2.7.1\IronPython.dll")
$py = [ironpython.hosting.python]::CreateEngine()
$pyv = $py.CreateScope()
$pyv.SetVariable("i",1)
$pyc = $py.CreateScriptSourceFromString("i += 1")
$pyc.Execute($pyv)
$pyc.Execute($pyv)
$pyv.GetVariable("i")

The result of i will of course be 3, as I ran the same Python code twice, to demonstrate that you can reuse a block of Python code in your PowerShell to do a repeating task. The variable comes back into PowerShell as an Int32 as expected. Thus, you can use Python to easily create objects or modify complex objects with less code. You can keep a library of Python scripts and use CreateScriptSourceFromFile() to load them into PowerShell for easy access.

One very odd variable transition to note, is that if you create a dict in Python, and move that variable into PowerShell, it will turn into a IronPython.Runtime.PythonDictionary collection, not an PSObject. Meaning, that you are unable to use Get-Member or Format-Table in PowerShell to display information on the object. Here is an example:

[reflection.assembly]::LoadFrom("C:\Program Files (x86)\IronPython 2.7.1\IronPython.dll")
$py = [ironpython.hosting.python]::CreateEngine()
$pyv = $py.CreateScope()
$pyc = $py.CreateScriptSourceFromString("d = {'one':1,'two':2}")
$pyc.Execute($pyv)
$d = $pyv.GetVariable("d")
$d.one
$d.two

If you enter in simply $d into your PowerShell, it will display all the keys. At first, I wasn't sure if it brought it over correctly from Python. Then I attempted to use GetType() on it, which didn't work, and neither did Get-Member. I was able to use $d.one to grab the value of the key however. I was unable to get the PowerShell For-Each to iterate through the keys, but I am sure that an Array should transfer over to PowerShell with no problems.

This concludes this article, so enjoy scripting PowerShell using Python, and the many Python libraries that exist. This is also my first post to utilize Pygments 1.5's new PowerShell Lexer, which actually looks rather good. Great job Pygments, I look forward to future improvements and Lexers.

About Me

My Photo
Names Kevin, hugely into UNIX technologies, not just Linux. I've dabbled with the demons, played with the Sun, and now with the Penguins.




Kevin Veroneau Consulting Services
Do you require the services of a Django contractor? Do you need both a website and hosting services? Perhaps I can help.

If you like what you read, please consider donating to help with hosting costs, and to fund future books to review.

Python Powered | © 2012-2013 Kevin Veroneau