prompt disappears during Event -Action

by stocksp at 2012-08-30 12:26:07

I’m watching file creation in a specific direcoty.
When my Register-ObjectEvent -Action function processes a file the command prompt doesn’t reappear when the function ends.
I can press ‘Enter’ to get it back. The -Action continues to be called whether or not the prompt is there.
Everything seems to run fine … just curious that maybe the non returning prompt is telling me something…

by DonJ at 2012-08-30 15:08:36
Moving this into Advanced Scripting to see if Kirk has any ideas…
by poshoholic at 2012-08-30 19:18:24
Would you mind sharing a simplified version of your script that reproduces this issue? That would help me figure out what’s going on more quickly.

Also are you running this in the native PowerShell console, or in another host? And just to confirm, is this PowerShell 2.0 or 3.0?
by stocksp at 2012-08-30 22:48:44
I’m running PS 3. and I see this behavior in both ISE and the regular PS window. ProcessFile is a function that does the HTML scraping.
I’ve pruned the code a lot, but the essence is here I would be glad to supply more or the ProcessFile if you like.

$fsw = [System.IO.FileSystemWatcher] $path

Register-ObjectEvent -InputObject $fsw –EventName Created -SourceIdentifier DDFileCreated -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
$fullPath = $Event.SourceEventArgs.FullPath

. ([Environment]::GetFolderPath(“Desktop”) + “\scripts\processFile.ps1”)

Write-Host “The file ‘$name’ was $changeType at $timeStamp path of $fullPath” -fore green
# is this one of interest
if($name -match(".+`.html"))
Write-Host “ready to processFile”
processFile $fullPath
Write-Host “End of Action”
} # end of -Action
by poshoholic at 2012-09-04 05:31:02
Have you compared the behaviour of this for html files vs. non-html files? I’m curious whether or not the prompt comes back if you’re not calling your processFile command. My gut tells me that processFile is probably triggering a change in the file (perhaps by tripping the LastModifiedDate or the LastAccessDate which may be enough to get the event to run again), resulting in an endless loop. That would explain your prompt not coming back. You might need to change how processFile does its work, being explicit about opening it read-only and not read-write (which may be the default internally?), or something similar.