File Watcher 使用 Windows Powershell 监控多个路径

File Watcher using Windows Powershell to monitor multiple paths

我正在构建一个 File Watcher Powershell 脚本来监视多个路径。我需要使用相同的 File Watcher 脚本监视大约 6 个路径。我在网上看到了一些脚本示例(“Powershell FileSystemWatcher for multiple paths”)并构建了一个脚本。按照脚本代码:

$i=0 

# list of path of the monitored folders. 

$paths = Get-Content "D:\abc\Folder_monitored_paths.txt"; 

foreach ($path in $paths)  
{ 

$filter = '*.*'   

$fsw = New-Object IO.FileSystemWatcher $path -Property @{IncludeSubdirectories = $true} 

Register-ObjectEvent $fsw Created -SourceIdentifier "$i+folderCreated" -Action { 

$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$fpath = $Event.SourceEventArgs.FullPath 
$timeStamp = $Event.TimeGenerated 

$logline = "$(Get-Date), $changeType, $fpath, $name"
Add-content "D:\log.txt" -value $logline 


}

$i = $i+1 

}

我已经放置了大约 3 条路径:$paths = Get-Content "D:\abc\Folder_monitored_paths.txt";

执行代码时,命令提示符立即 returns 返回到输出屏幕。之后,当我将文件放在所有 3 个路径中时,它会记录所有 3 个目录的条目。在第二个 运行 上,当我再次将文件放入目录时,日志文件中没有记录任何条目。为什么会这样?为什么它不第二次记录条目,为什么命令提示符立即 returns 返回?我在代码中缺少/错误的东西?

对我的上述 2 个查询没有任何线索。请帮忙。

根据我上一条评论的 OP。

$i=0 

# list of path of the monitored folders. 

$paths = Get-Content "D:\temp\Folder_monitored_paths.txt" 

foreach ($path in $paths)  
{ 
    $filter = '*.*'   

    $fsw = New-Object IO.FileSystemWatcher $path -Property @{IncludeSubdirectories = $true} 

    Register-ObjectEvent $fsw Created -SourceIdentifier "$i+folderCreated" `
    -Action { 
                $name = $Event.SourceEventArgs.Name 
                $changeType = $Event.SourceEventArgs.ChangeType 
                $fpath = $Event.SourceEventArgs.FullPath 
                $timeStamp = $Event.TimeGenerated 

                $logline = "$(Get-Date), $changeType, $fpath, $name"
                Add-content "D:\temp\log.txt" -value $logline 
            }

    $i = $i+1 
}

<#
Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
1      0+folderCreated                 NotStarted    False                                 ...                     
2      1+folderCreated                 NotStarted    False                                 ...                     
3      2+folderCreated                 NotStarted    False                                 ... 
#>


Get-EventSubscriber -Force | Format-Table -AutoSize
<#
SubscriptionId SourceObject                EventName SourceIdentifier Action                                  HandlerDelegate SupportEvent ForwardEvent
-------------- ------------                --------- ---------------- ------                                  --------------- ------------ ------------
             1 System.IO.FileSystemWatcher Created   0+folderCreated  System.Management.Automation.PSEventJob                        False        False
             2 System.IO.FileSystemWatcher Created   1+folderCreated  System.Management.Automation.PSEventJob                        False        False
             3 System.IO.FileSystemWatcher Created   2+folderCreated  System.Management.Automation.PSEventJob                        False        False
#>

<#
11/20/2018 17:10:31, Created, d:\temp\abcpath0\New Text Document.txt, New Text Document.txt
11/20/2018 17:10:42, Created, d:\temp\abcpath1\New Text Document.txt, New Text Document.txt
11/20/2018 17:10:47, Created, d:\temp\abcpath2\New Text Document.txt, New Text Document.txt
11/20/2018 17:11:08, Created, d:\temp\abcpath0\New folder, New folder
11/20/2018 17:11:13, Created, d:\temp\abcpath1\New folder, New folder
11/20/2018 17:11:18, Created, d:\temp\abcpath2\New folder, New folder
11/20/2018 17:12:29, Created, d:\temp\abcpath0\New folder\New Text Document.txt, New folder\New Text Document.txt
11/20/2018 17:12:34, Created, d:\temp\abcpath1\New folder\New Text Document.txt, New folder\New Text Document.txt
11/20/2018 17:12:41, Created, d:\temp\abcpath2\New folder\New Text Document.txt, New folder\New Text Document.txt
11/20/2018 17:12:46, Created, d:\temp\abcpath0\New folder\New folder, New folder\New folder
11/20/2018 17:12:52, Created, d:\temp\abcpath1\New folder\New folder, New folder\New folder
11/20/2018 17:13:00, Created, d:\temp\abcpath2\New folder\New folder, New folder\New folder
11/20/2018 17:13:29, Created, d:\temp\abcpath0\New Rich Text Document.rtf, New Rich Text Document.rtf
11/20/2018 17:13:42, Created, d:\temp\abcpath1\New Rich Text Document.rtf, New Rich Text Document.rtf
11/20/2018 17:13:47, Created, d:\temp\abcpath2\New Rich Text Document.rtf, New Rich Text Document.rtf
11/21/2018 17:52:39, Created, d:\temp\abcpath0\New Microsoft Excel Worksheet.xlsx, New Microsoft Excel Worksheet.xlsx
11/21/2018 17:52:39, Created, d:\temp\abcpath0\~ew Microsoft Excel Worksheet.tmp, ~ew Microsoft Excel Worksheet.tmp
11/21/2018 17:52:39, Created, d:\temp\abcpath0\New Microsoft Excel Worksheet.xlsx~RF82e46c.TMP, New Microsoft Excel Worksheet.xlsx~RF82e46c.TMP
11/21/2018 17:52:45, Created, d:\temp\abcpath1\New Microsoft Excel Worksheet.xlsx, New Microsoft Excel Worksheet.xlsx
11/21/2018 17:52:48, Created, d:\temp\abcpath2\New Microsoft Excel Worksheet.xlsx, New Microsoft Excel Worksheet.xlsx
11/21/2018 17:52:56, Created, d:\temp\abcpath2\New Bitmap Image.bmp, New Bitmap Image.bmp
11/21/2018 17:53:03, Created, d:\temp\abcpath1\New Bitmap Image.bmp, New Bitmap Image.bmp
11/21/2018 17:53:05, Created, d:\temp\abcpath0\New Bitmap Image.bmp, New Bitmap Image.bmp
#>

Get-EventSubscriber -Force | Unregister-Event -Force