/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" PowerShell RC2 and Active Directory "," PowerShell : Using IronPython to Connect to AD and... "," PowerShell Session video of Bruce Payette "," PowerShell : Active Directory Part 11 - moving - R... "," PowerShell : Learn about the HashTable Object and ... "," PowerShell : Setting SendAs permission in Exchange... "," PowerShell : Active Directory Part 10 (AD Browser)... "," PowerShell "," PowerShell : How Do I randomize a list, and remove... "," PowerShell : Can you do that less cryptic ? "," ")

Friday, September 29, 2006

 


PowerShell : Hosting IronPython



In last post about using IronPython from PowerShell : Using IronPython to Connect to AD and list children , I did just use the CLI.

In this post I will show how to Use IronPython from Powershell, by hosting it.

I first load the DLL's needed (copy them to PSHOME directory), and then just start using it.

 

[System.Reflection.Assembly]::LoadFile("$pshome\IronPython.dll")
[System.Reflection.Assembly]::LoadFile("$pshome\IronMath.dll")

$ipe = new-object IronPython.Hosting.PythonEngine 

$code = @'

import sys
import clr
import System

clr.AddReferenceByPartialName("System.DirectoryServices")

import System.DirectoryServices

from System.DirectoryServices import *

root = DirectoryEntry()

for c in root.Children: print c.Name


u = root.Children.Find("CN=FooBar")

for p in u.Properties: print p.PropertyName , p.Value

for p in u.Properties.__getitem__("CN"): print p


'@

$ipe.Execute($code)

 

 

The results are not lost you can still work with them and you can exchange variables very easy also see examples below :

 

 

# You can get back the results by using Evaluate

PS C:\PowerShell> $ipe.Evaluate('root.Children.Find("CN=FooBar")')

distinguishedName
-----------------
{CN=fooBar,DC=mow,DC=local}

# Or you can get the variable

PS C:\PowerShell> $ipe.Globals["u"]

distinguishedName
-----------------
{CN=fooBar,DC=mow,DC=local}

# Set a IronPython Variable

PS C:\PowerShell> $ipe.Globals['domain'] = [adsi]''
PS C:\PowerShell> $ipe.Evaluate('domain.Children.Find("CN=FooBar")')

distinguishedName
-----------------
{CN=fooBar,DC=mow,DC=local}

# and do a Dir (get-member) on that variable in IronPython

PS C:\PowerShell> $ipe.Evaluate('dir(domain)')
AuthenticationType
CanRaiseEvents
Children
Close
CommitChanges

 

As you see its very easy to mix  IronPython and PowerShell parts.

Realy great ! 

 

 

Enjoy,

Greetings, /\/\o\/\/

Tags : Monad IronPython PowerShell




Comments:
Blogger gbraad
This doesn't seem to work with PowerShell 1.0 and IronPython 1.0.1. The constructor will result in an exception...
 
Post a Comment

Links to this post:

Create a Link



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?