locked
PowerShell FileWatcher (System.IO.FileSystemWatcher) is not executing the python script RRS feed

  • Question

  • Hello Everyone,

    I tried with different options, and searched a lot of blogs to get more details on the execution of python script within powershell FileWatcher script. The powershell FileWatcher could able to execute only powershell specific commands when the event is triggered, but not python script. Please help me out to execute the python script when the event is triggered.

    $dir = 'C:\Users\XXXX\Documents\FileWatcher' 
    $filter = '*.*' 

    $Obj = New-Object IO.FileSystemWatcher $dir, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters] 'FileName, LastWrite'}

    Register-ObjectEvent $Obj Created -SourceIdentifier FileCreated -Action {

    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated

    ## Executing python script  ## test.py and this powershell script are located in same folder
    C:\\Users\\XXXX\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe test.py  ## --> tried with commands as well==>     python test.py ###  python.exe test.py 

    }
    $Obj.EnableRaisingEvents = $true
      do
      {
        Wait-Event -Timeout 5

      } while ($true)

                                     


    Tuesday, July 14, 2020 9:35 PM

All replies

  • $action = {
        Write-Host $Event.SourceEventArgs.Name $Event.SourceEventArgs.ChangeType $Event.TimeGenerated -fore green
    }
    
    $path = 'C:\Users\XXXX\Documents\FileWatcher'
    $fsw = [System.IO.FileSystemWatcher]::New($path)
    $fsw.NotifyFilter = 'FileName,LastWrite'
    $fsw.EnableRaisingEvents = $true
    
    Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action $action
    while($true){
        sleep 1 # Ctrl-C to exit
    }


    \_(ツ)_/

    Tuesday, July 14, 2020 9:56 PM
  • Hello Jrv,

    Thank you for your code snippet.

    The code was not executing python script  if I added "python script run" statement in the action block.
     I add python statement as shown below.

    $action = {
        Write-Host $Event.SourceEventArgs.Name $Event.SourceEventArgs.ChangeType $Event.TimeGenerated -fore green
    #python test.py 200
    python C:\Users\XXXXX\Documents\sprint15\\umd_master.py     C:\Users\XXXX\Documents\sprint15\\params.json
    }

    Please let me know if you have any thoughts on this to fix this issue.

    Tuesday, July 14, 2020 10:39 PM
  • How do you know it isn't running?  You will not get any errors on the screen.


    \_(ツ)_/

    Tuesday, July 14, 2020 10:58 PM
  • Actually, the python script executes one function, and stores the results into an output file. If filewatcher script works fine whenever create event occurs in the given folder, then python script should have generated a file with output results. But, there was no output file found as expected.


    Tuesday, July 14, 2020 11:05 PM
  • Not true.  If the python script generates an exception then nothing will be output.

    Place a write-host after the python call to see if it is executed.


    \_(ツ)_/

    Tuesday, July 14, 2020 11:44 PM
  • This will help you understand how this works.

    $action = {
        Try{
            Write-Host $Event.SourceEventArgs.Name $Event.SourceEventArgs.ChangeType $Event.TimeGenerated -fore green
            1/0
            Write-Host "`t`tCOMPLETED" -fore green
        }
        Catch{
            Write-Host $_ -fore red
        }
    }
    $path = 'C:\Users\XXXX\Documents\FileWatcher'
    $fsw = [System.IO.FileSystemWatcher]::New($path)
    $fsw.NotifyFilter = 'FileName,LastWrite'
    $fsw.EnableRaisingEvents = $true
    
    $job = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action $action
    while($true){
        sleep 1 # Ctrl-C to exit
    }
    


    \_(ツ)_/

    Tuesday, July 14, 2020 11:54 PM