Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

Using IronPython from PowerShell Part 1 : Watch folder for changes without blocking Console

This is the first part of a 6 part series about using an Hosted IronPython Engine from PowerShell to do some tasks PowerShell does not yet support in V1.

In PowerShell you can very easy host a IronPython engine just by loading the DLL's , see this post on my old blog PowerShell : Hosting IronPython

I use it so solve some shortcomings in PowerShell 1.0 as Async Event watching, Threading support and using controls that need STA mode.

I do use it so much that I added the following lines to my profile to load them standard, so I have always a IronPython engine handy.

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

$ipe = new-object IronPython.Hosting.PythonEngine

I did show how easy it is how to interact with IronPython on my old blog in PowerShell : Hosting IronPython  and since that time I did work out some more IronPython solutions to solve some problems I did run into in PowerShell with those Shortcomings in V1 of PowerShell

In this series I will show some examples of the solutions to those problems using the hosted IronPython Engine.

I will cover the following topics :

  1. Events : Using FileSystemWatcher without blocking PowerShell Console (This Post )
  2. STA mode : Using the Clipboard from PowerShell
  3. Threading : Using a Form Without Blocking the PowerShell Console
  4. Events / NotifyIcon : Using Notify Icon and Popup balloon for Events (non blocking version of /\/\o\/\/ PowerShelled: MSH directory watcher with popup-balloon using IP )
  5. STA / Interaction : Example of Controlling a IronPython Form from PowerShell (Browser Example )
  6. WrapUp : some more examples, overview of added value and how to go on making use of it  (combine with PowerShell GUI ScripBlock Monitor Script )  

In this first Post I will show how to Use the IronPython Engine to :

 Watch a Folder for Changes from PowerShell using the filesytemWatcher Object without blocking the Console.

Problem :

As I mentioned in this post on my old blog /\/\o\/\/ PowerShelled: MSH directory watcher with popup-balloon  there are some troubles with using the Filewatcher class for monitoring file changes in PowerShell by chatching the Events.


Main problem is that the filewatcher is on another Thread, hence you can't (yet) use a delegate .

"Jeff Jones [MSFT]" wrote:
This is a known issue where if the
ScriptBlock gets called on a different thread then it cannot be invoked.
It looks like the plan for V1 is to improve the error message and then possibly
take a fix in V2.

you still can wait for an event to happen by doing this :
$fsw = New-object System.IO.FileSystemWatcher
$r = new-object System.IO.WaitForChangedResult
$r = $fsw.WaitForChanged('all')


but then you can't quit no more !! (only with ctrl-break but that will exit PowerShell )

A workaround a made for this was :

while ($True) {
write-host -noNewLine "."
$fsw = New-object System.IO.FileSystemWatcher
$r = new-object System.IO.WaitForChangedResult
$fsw.path = $args[0]
$r = $fsw.WaitForChanged('all',10000)
if ($r.timedOut -eq $False){
$changed = $True
$r fl out-string
}
}


so you can quit every 10 seconds, But the looping is not very elegant, also we are still blocking our shell,

Solution :

In this first part of this series, I will show how we can use IronPython to solve this problems so we can start the FileSytemWatcher and it will not block the interface so we can work on and when a event happens we will get an message in the PowerShell console (in Part 4 of this series I also will show a IronPython Version of the Popup balloon Script.)

The Script looks like this (* note * you need to load the IronPython DLL's and create $IPE first See above ):

 

# IronPyton EventWatcher example

$code = @'
from System.IO import *
w = FileSystemWatcher()
dir(w)
w.Path = "c:\PowerShell"
def handle(w, a): print a.ChangeType, a.FullPath
w.Changed += handle
w.Created += handle
w.Deleted += handle
w.EnableRaisingEvents = True
'@
$ipe.Execute($code)

 

 after loading the IronPython DLL's and creating the Engine ( $ipe ) you can just paste in the code above on your console (Change directory to watch to targetdirectory first, In part 3 of this series I will show how you can pass it in as a parameter from PowerShell) 

 As you can see in the Following examples after that, you can just work on but if a file does change you will get a message on the PowerShell Console.  

 

PoSH> $code = @'                                                                                                        
>> from System.IO import *                                                                                              
>> w = FileSystemWatcher()                                                                                              
>> dir(w)                                                                                                               
>> w.Path = "c:\PowerShell"                                                                                             
>> def handle(w, a): print a.ChangeType, a.FullPath                                                                     
>> w.Changed += handle                                                                                                  
>> w.Created += handle                                                                                                  
>> w.Deleted += handle                                                                                                  
>> w.EnableRaisingEvents = True                                                                                         
>> '@                                                                                                                   
>> $ipe.Execute($code)                                                                                                  
>>                                                                                                                      
PoSH> New-Item -ItemType file TestFile.txt                                                                              
Created c:\PowerShell\TestFile.txt                                                                                      
                                                                                                                        
                                                                                                                        
    Directory: Microsoft.PowerShell.Core\FileSystem::C:\PowerShell                                                      
                                                                                                                        
                                                                                                                        
Mode                LastWriteTime     Length Name                                                                       
----                -------------     ------ ----                                                                       
-a---         2/10/2007   8:30 PM          0 TestFile.txt                                                               
                                                                                                                        
                                                                                                                        
PoSH> 1..5 |% { $_;"$_" | sc c:\powerShell\TestingEvent.txt}                                                            
1                                                                                                                       
Changed c:\PowerShell\TestingEvent.txt                                                                                  
Changed c:\PowerShell\TestingEvent.txt                                                                                  
2                                                                                                                       
Changed c:\PowerShell\TestingEvent.txt                                                                                  
Changed c:\PowerShell\TestingEvent.txt                                                                                  
3                                                                                                                       
Changed c:\PowerShell\TestingEvent.txt                                                                                  
Changed4 c:\PowerShell\TestingEvent.txt                                                                                 
                                                                                                                        
Changed c:\PowerShell\TestingEvent.txt                                                                                  
Changed5 c:\PowerShell\TestingEvent.txt                                                                                 
                                                                                                                        
Changed c:\PowerShell\TestingEvent.txt                                                                                  
Changed c:\PowerShell\TestingEvent.txt                                                                                  
PoSH> $ipe.Globals['w'].enableRaisingEvents = $false                                                                    
PoSH> 1..5 |% { $_;"$_" | sc c:\powerShell\TestingEvent.txt}                                                            
1                                                                                                                       
2                                                                                                                       
3                                                                                                                       
4                                                                                                                       
5                                                                                                                       
PoSH>                                                                  

 As you can see from the examples above , if a file does change, the messages will come in directly without waiting , no matter what is happening in the Console , so this can mess up the console, and your current line a bit, note the numbers 4 and 5 in the example, ( I did color them red to make it easier to find)

I sometimes even get them in my prompt ;-) ,

but hey we asked for that didn't we ? and if the event is important enough who cares ? I don't ;-)

but in Part 3 I also will show how to use the Popup Balloon as in my PowerShell version, that also does not block the Shell.

In the next part I will Show how we can use the Clipboard , that needs an STA thread  and PowerShell runs in MTA mode, from PowerShell using IronPython

For more info see this posts on my old blog :

Problem description : /\/\o\/\/ PowerShelled: playing with MY-object from MSH 

workarounds : /\/\o\/\/ PowerShelled: MSH Clipboard use workaround (workaround hosting VB.NET )

and PowerShelled- MSH Clipboard part 2 (of 3 -) (using VB.NET DLL) )

 

Enjoy,

 Greetings /\/\o\/\/

Published Saturday, February 10, 2007 2:27 PM by admin

Comments

# re: Using IronPython from PowerShell Part 1 : Watch folder for changes without blocking Console

Lol, you suck sweeping through the Advanced division like that so quickly! :)

I admit, I am enjoying the scripting games immensely so far, even if I'm focusing on completing the Beginner section and dabbling in the Advanced part. My goal is a perfect Beginner score, and to at least complete half the Advanced ones and give a best effort on the others such that I understand the answers when they come out.

=)

Tuesday, February 13, 2007 1:59 PM by LonerVamp

# re: Using IronPython from PowerShell Part 1 : Watch folder for changes without blocking Console

Hi LonerVamp,

Thanks for your comment,

and nice to see you will publish your posts

I did send them in all 10 now, and also made some more advanced versions of some I will post on my blog when the anwers will arive.

As it's not fair the VbSCript Guy's may make a GUI on event 10 and the PowerShell Guy's only a Text version, so that is one you can expect on my blog ;-)

Have a good games !

Greetings /\/\o\/\/

Tuesday, February 13, 2007 3:46 PM by MoW

# re: Using IronPython from PowerShell Part 1 : Watch folder for changes without blocking Console

That's awesome! I look forward to seeing those posts!

Tuesday, February 13, 2007 5:47 PM by LonerVamp

# Using IronPython from PowerShell Part 2 : Clipboard (STA mode)

This is the Second part of a 6 part series about using an Hosted IronPython Engine from PowerShell to

Tuesday, February 20, 2007 1:46 PM by The PowerShell Guy

# Hey, PowerShell Guy ! How Can I Monitor Event Log Messages for Specific Words?

In the Hey Scripting Guy article : How Can I Monitor Event Log Messages for Specific Words? , a WMI eventwatcher

Monday, February 26, 2007 3:40 PM by The PowerShell Guy

# PowerShell Eventing Library : PSEventing 0.5 Beta released

PSEventing 0.5 Beta released !! PSeventing is a very powerful PowerShell Snapin providing a library for

Wednesday, May 09, 2007 7:35 PM by The PowerShell Guy

# re: Using IronPython from PowerShell Part 1 : Watch folder for changes without blocking Console

This seems to be broken in IronPython 2.0 Alpha 2. I can load the assembly but I cannot create a PythonEngine object.

Code:

[void] [System.Reflection.Assembly]::LoadFile("C:\IronPython-2.0A2\IronPython.dll")

$ipe = New-Object "IronPython.Hosting.PythonEngine"

Friday, July 06, 2007 10:50 PM by gelyon

# re: Using IronPython from PowerShell Part 1 : Watch folder for changes without blocking Console

Still using the "old" version I will try it later

thanks for the comment

Greetings /\/\o\/\/

Wednesday, July 11, 2007 9:16 AM by MoW

# re: Using IronPython from PowerShell Part 1 : Watch folder for changes without blocking Console

Hi,

great peace of code, but I failed too creating python object with powershellV1 and python 1.1.0

Thursday, October 18, 2007 9:24 AM by Antoine

# ??? ??? ??????? ???? .... ? ??? ?? ??? ???? - ????? ?????? ??????

Anonymous comments are disabled