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).

1 Answer 1

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()
}
Sign up to request clarification or add additional context in comments.

2 Comments

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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.