0

到目前为止,这是 PowerShell 脚本:

它可以很好地监视指定的文件夹,但会在文件更改时生成两个条目。

有没有办法合并或只返回一个事件?

Get-EventSubscriber | Unregister-Event

$fsw = New-Object System.IO.FileSystemWatcher

$fsw.Path = 'c:\ftptest'
$fsw.Filter = '*.*'
$fsw.EnableRaisingEvents = $true
$fsw.IncludeSubdirectories = $true
$fsw.NotifyFilter = [IO.NotifyFilters]::LastWrite


$null = Register-ObjectEvent -InputObject $fsw -EventName Changed -SourceIdentifier MonitorFiles 

while ($true)
{
    $evt = Wait-Event -SourceIdentifier MonitorFiles

    Write-Host "Changed: $($evt.SourceEventArgs.FullPath)"

    $evt | Remove-Event

}

当我编辑文件并保存它时,它会输出两次更改的文件。已更改:c:\ftptest\Test1.txt 已更改:c:\ftptest\Test1.txt

好的,我已将代码更改为似乎有帮助的代码-如果您愿意的话,这是一种触发器...

Get-EventSubscriber | Unregister-Event

$fsw = New-Object System.IO.FileSystemWatcher

$fsw.Path = 'c:\ftptest'
$fsw.Filter = '*.*'
$fsw.EnableRaisingEvents = $true
$fsw.IncludeSubdirectories = $true
$fsw.NotifyFilter = [IO.NotifyFilters]::LastWrite


$null = Register-ObjectEvent -InputObject $fsw -EventName Changed -SourceIdentifier MonitorFiles 

$Buffer = ""

while ($true)
{
    $evt = Wait-Event -SourceIdentifier MonitorFiles

    if ($Buffer -eq "Changed: $($evt.SourceEventArgs.FullPath)") {

        $Buffer = ""

    } Else {

        $Buffer = "Changed: $($evt.SourceEventArgs.FullPath)"

    }

    Write-Host $Buffer.ToString()

    $evt | Remove-Event

}
4

0 回答 0