Home > Net >  PowerShell IO.FileSystemWatcher doesn't work after restart fileserver
PowerShell IO.FileSystemWatcher doesn't work after restart fileserver

Time:10-15

I need a FileSystemWatcher on a network directory (fileserver)

The script works well but fails after a reboot from the fileserver.

How can I detect if the FSW is failing and restart the watcher if the fileserver is up and running again?

Code:

$destinations = @{"\\location1" = "c:\destination1"
                    "\\location2" = "c:\destination2"       
        }

foreach ($location in $destinations.Keys) {       

    $Watcher = New-Object IO.FileSystemWatcher -Property @{
        Path                  = $location
        Filter                = "*.*"
        IncludeSubdirectories = $false
        NotifyFilter          = [IO.NotifyFilters]'FileName, LastWrite'
       }

    Register-ObjectEvent $Watcher -EventName Created -SourceIdentifier $location -Action {
    
        $path = $Event.SourceEventArgs.FullPath
        $name = $Event.SourceEventArgs.Name
        $changeType = $Event.SourceEventArgs.ChangeType
        $timeStamp = $Event.TimeGenerated
        $SI = $Event.SourceIdentifier
        Write-Host "The file '$name' was $changeType at $timeStamp"
        Write-Host $path
        Move-Item $path -Destination $destinations[$SI] -Force -Verbose
    }  
   
}

CodePudding user response:

RTFM!

There is Error event from FileSystemWatcher, and there is your case exactly described.

For example, if the object is monitoring changes in a remote directory and the connection to that directory is lost, the Error event is raised.

CodePudding user response:

Solution found:

$destinations = @{"\\location1" = "c:\destination1"
                    "\\location2" = "c:\destination2"       
        }

foreach ($location in $destinations.Keys) {       


      $Watcher = New-Object IO.FileSystemWatcher -Property @{
        Path                  = $location
        Filter                = "*.*"
        IncludeSubdirectories = $false
        NotifyFilter          = [IO.NotifyFilters]'FileName, LastWrite'
       }

     $action = {
        $path = $Event.SourceEventArgs.FullPath
        $name = $Event.SourceEventArgs.Name
        $changeType = $Event.SourceEventArgs.ChangeType
        $timeStamp = $Event.TimeGenerated
        $SI = $Event.SourceIdentifier
        Write-Host "The file '$name' was $changeType at $timeStamp"
        Write-Host $path
        Move-Item $path -Destination $destinations[$SI] -Force -Verbose
    }


    $actionError = {
   
                   $Sender.EnableRaisingEvents = $false
                    $ip = $location -split "\\" | Where {  $_ -ne ""  } | Select -first 1
                   do { 
                        Write-Host "Waiting for boot "   $ip
                        Start-Sleep -Seconds 5 
                    } until (Test-Connection $ip -Quiet -Count 1) 

                   $Sender.EnableRaisingEvents = $true

         }

    Register-ObjectEvent $Watcher -EventName Created -SourceIdentifier $location -Action $action
    Register-ObjectEvent $Watcher -EventName Error -SourceIdentifier $location 'ERROR' -Action $actionError

 

}
  • Related