3

I have a FileSystemWatcher program in PowerShell that is supposed to run on a server as long as the server is on. The program is started when the server starts. The FSW is supposed to run a program each time a new file is added to the folder it is watching, which is on a network drive. But for some reason, it doesn't execute the "action" program after some time. After a restart, the program works fine, running the "action" each time a new file arrives. I have not been able to find a clear pattern - it seems to stop responding, sometimes after a day, other times after just one "firing" of the action program.

I suspect this is because I am watching a file on a network drive, which according to other threads on stackoverflow, is unreliable and might need resetting: FileSystemWatcher stops catching events

The provided link sovles it in C#, so I wonder if a similar resetting could be written in Powershell?

Here is the program I am trying to run. I am working on a try catch for the fsw, but from what I have gathered so far, the problem must likely be solved by resetting if the network connection is interrupted.

$ErrorActionPreference = "Stop"

$action  = "someprogram.ps1"

function log($string, $color, $logfile)
{
   if ($Color -eq $null) {$color = 'white'}
   write-host $string -foregroundcolor $color
   $string | out-file -Filepath $logfile -append
}


$folder = "somefolder"
$filter = '*.csv'  


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

$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action 
        {  
        $global:FilePath = $Event.SourceEventArgs.FullPath
        $time = get-date
        log -String "File $FilePath detected at $time" -logfile $logfile
        & $action 
        }

I am thinking a solution like this would be preferred to running a C# code:

while (Test-Path -Path $folder)
{
Start-Sleep -Milliseconds 100 #to reduce cpu load
##FileSystemWatcher code here
}
##Somehow restart file watcher if connection is lost

However, I dont want the script to run again every second or so. So I am thinking I might have to have another script running in parralell that checks if the folder path exists, and upon a disconnect, run a

Unregister-Event -SourceIdentifier FileCreated

and then restart the script. Then again, what happens if the connection to the folder is broken for one millisecond, while my script is sleeping? In that case, the test-path will not notice anything, and the script will fail as the filewatcher will no longer be able to detect a new file

Community
  • 1
  • 1
Ullsokk
  • 697
  • 2
  • 11
  • 24
  • 1
    Any C# program can run in PowerShell. Obviously you can compile it to `.exe` and run it. Or you can load an assembly directly into PS. – Ryan Bemrose Mar 04 '16 at 08:59
  • I am also struggling with this and cannot determine how to do this without calling C# from PowerShell. The most important piece we need from C# is something like `fsw.Error += new ErrorEventHandler(OnErrorMethod);` – Robert Hartshorn Mar 01 '18 at 16:26

0 Answers0