FIleSystemWatcher监控挂起

时间:2018-02-15 15:56:55

标签: .net powershell filesystemwatcher administration orchestration

我试图在PowerShell中基于来自.Net的FileSystemWatcher创建一个ftp orchestrator,然后运行其他PowerShell脚本来处理这个文件。

但是当监视器意外挂断,并且不再有效(应记录它的操作 - 具有记录模块)时,我遇到了问题。

我找不到模式,因为有时它可以工作3天,有时候工作~1天,最近它在工作40分钟后就停止了。 不要认为这是处理文件的情况,因为我的日志包含32 KB到32 MB; /.

我有默认的InternalBufferSize。

如何获取有关此问题根源的信息?

如何增加所有组件的详细程度以了解发生了什么?

try {
        $fswObject = New-Object IO.FileSystemWatcher "$folderToMonitor", $eventData.matchingFileMask -Property @{IncludeSubdirectories = $eventData.includeSubdirectories; NotifyFilter = [IO.NotifyFilters]'FileName,LastWrite'}
        $fswObject.EnableRaisingEvents = $True
        $sourceIdentifier = "$($sourceIdentifierId)_$($eventData.matchingFileMask)" # to make source identifier unique
        $existingSubscription = Get-EventSubscriber -SourceIdentifier $sourceIdentifier -ErrorAction SilentlyContinue
        if ( $existingSubscription ) {
            Write-LogWarn "($PID) Subscription '$($existingSubscription.SourceIdentifier)' exists, unregistering"
            Unregister-Event -SourceIdentifier $sourceIdentifier
        }
        Write-LogDebug "($PID) Registering watcher in '$folderToMonitor' for file matching $($eventData.matchingFileMask) with source identifier $sourceIdentifier"
        $dataToPass = New-Object PSObject -property @{
            matchingFileMask = $eventData.matchingFileMask;
            startProgram     = $eventData.startProgram.Trim();
            folderToMonitor  = $folderToMonitor
        }

        Register-ObjectEvent $fswObject $eventData.triggeringFileEvent -SourceIdentifier $sourceIdentifier -MessageData $dataToPass -Action {
            Param($fswObject = $fswObject)

            $name = $Event.SourceEventArgs.Name
            $fullname = $Event.MessageData.folderToMonitor + "\" + $name
            $changeType = $Event.SourceEventArgs.ChangeType
            $timeStamp = $Event.TimeGenerated.toString("yyyy-MM-dd HH:mm:ss.fff")
            $object_type = (Get-Item $fullname).GetType().name -replace "Info", ""
            Write-LogInfo "($PID) [$object_type] '$fullname' was $changeType at $timeStamp for mask $($Event.MessageData.matchingFileMask)"
            if ( $Event.MessageData.startProgram -ne "" ) {
                $startProgram = $Event.MessageData.startProgram.split(" ")[0]
                $programParams = $Event.MessageData.startProgram.Substring( $startProgram.Length ).Trim()
                try {
                    # will start new parallel job
                } catch {
                    Write-LogError "($PID) Error when executing $PSScriptRoot\$startProgram $programParams $($Event.MessageData.folderToMonitor)\$name, error is '$Error'"
                }
            }
        }
    }

0 个答案:

没有答案