[IronPython] Writing PowerShell cmdlets in Python

Curt Hagenlocher curt at hagenlocher.org
Sun Apr 13 05:21:12 CEST 2008


http://hagenlocher.org/software/PythonCmdlet.zip

 This is a sample project that shows one approach to creating a PowerShell
cmdlet using IronPython.  The cmdlet object has to derive from
System.Management.Automation.Cmdlet, it needs to override particular virtual
functions and it needs appropriate attributes on the class and on the
properties representing the cmdlet's parameters.

Originally, I was hoping to use the project I'm working on[1] to
automatically generate an appropriate wrapper from the Python class.
Unfortunately, I haven't yet been able to think of a sufficiently clever
scheme to handle the scenario where the wrapper needs to be derived from a
specific base class, and the Python object can transparently access the
members of that same base class despite not having the same object identity.

Here, I've taken the approach of passing the actual Cmdlet-derived class as
a parameter to the constructor of the Python class.  The Python script
itself is stored as a resource in the cmdlet.  The cmdlet implements a
"browse-matches" command that is designed to have MatchInfo objects
pipelined into it.  Usage would be something like this:

select-string NewTypeMaker | browse-matches

The results are displayed as simple strings in a listbox on a modal form.

Information on installing and using a PowerShell cmdlet can be found (among
other places) at
http://blog.cumps.be/using-powershell-writing-and-debugging-cmdlets/

 This project is based on IronPython 2.0B1.


1: http://www.codeplex.com/coils -- not forgotten, but currently held up by
an unresolved "strongly-named assembly is required" that I'm trying to work
around.

 --
Curt Hagenlocher
curt at hagenlocher.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20080412/d3e4089a/attachment.html>


More information about the Ironpython-users mailing list