1

I am creating a quick temporary fix so sorry if this is dirty, but-

Objective

I would like to use Powershells Register-Event cmd to wait for a file dropped in a folder, then call a function that parses the file and outputs it to excel. I don't need help with the coding aspect, just the concept. It is still a little mysterious to me as of where this Event is running and what resources it has to work with.

Things I've Tried

  1. One .ps1 file with registered events at the bottom, calling a function at the top, called by a batch file.
    Behavior: Stops on this line:

    $sr = new-object System.IO.StreamReader($copyPath)
    

    This is my first invocation of .NET, so this is why I was assuming it is an issue with .NET.

  2. Two .ps1 files, FileWatcher and Parser, both work great when run separately, called by a batch file.
    Behavior: FileWatcher Outputs "This Line" but fails to output any lines in Parser, and never gets to that line.

    Register-ObjectEvent $fsw Changed -SourceIdentifier FileChange -Action {
      Write-Host "This Line"
      .\src\Parser.ps1
      Write-host "That Line"
    }
    
  3. I even got as desperate as to go to two ps1 files and two batch files. Lets just say it didn't work.

Generic batch file command I am using:

powershell.exe -noexit C:\scripts\src\FileWatcher.ps1

Questions

Why does certain commands run fine when called from a registered event, and other commands like .NET not work?

Is what I am trying to achieve even possible?

Do you have a better way to achieve my objective? (Scripting only, remember this is a hotfix).

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Cole9350
  • 5,444
  • 2
  • 34
  • 50

1 Answers1

3

The following worked for me (code mostly copied from here):

$folder = 'c:\Temp'
$filter = '*.*'

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

Register-ObjectEvent $monitor Created -SourceIdentifier FileCreated -Action {
  $name = $Event.SourceEventArgs.FullPath
  $sr = New-Object System.IO.StreamReader($name)
  Write-Host $sr.ReadToEnd()
  $sr.Close()
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Hey thanks, you showed me that it is not a .net problem at all. I figured out my line was getting to an error and stopping. Also, now that I got it finally all to run, it outputs verbose statements but seems like its keeping all write-host statements in a stack and outputting them all at the end. I would like the code to handle errors smoothly and keep running like it normally would if I ran it outside a Event. I tried setting $ErrorActionPreference = Continue . Also, I'm still looking for a simple explanation on how these processes work. I just crashed my work pc due to hundreds of PS process – Cole9350 Jun 25 '13 at 19:30
  • Closing out question because original problem was resolved. Could only make this work by adding error checking everywhere. Please reply if you have a better solution. – Cole9350 Jul 02 '13 at 17:13