[英]Register-ObjectEvent of System.IO.FileSystemWatcher not working as expected
[英]Register-ObjectEvent cmdlet not working properly on Powershell but working on ISE
我正在使用 Powershell 脚本来监视文件夹,当创建新项目时,脚本需要将该文件复制到另一个文件夹。
我遇到的问题是,当我在 Powershell ISE 中执行它时,它运行良好,但是在 Powershell 上执行它时,它仅在 Powershell 窗口打开的时间段内有效(> 1 秒)。
我尝试将 sleep 命令放在最后,发现只有在脚本结束时才会执行操作,在这种情况下,当我在 Powershell 中按 CTRL+C 停止脚本时,我应该执行的操作创建的项目一起执行。
不知道是我做错了什么还是只是误解了什么。
这是我用来测试它的脚本:
$Watcher = New-Object System.IO.FileSystemWatcher
$Watcher.path = "\\192.168.5.127\data\TestWatcher"
$Destination = "C:\TestWatcher"
$Watcher | Get-Member -MemberType Event
$Watcher.EnableRaisingEvents = $true
$action = {
$path = $event.SourceEventArgs.FullPath
$name = $event.SourceEventArgs.Name
$changetype = $event.SourceEventArgs.ChangeType
Write-Host "File $name at path $path was $changetype at $(get-date)"
Copy-Item $Watcher.path $Destination
}
Register-ObjectEvent $Watcher 'Created' -Action $action
任何帮助或建议将不胜感激。
此致,
Gregor y在评论中提供了关键提示:
您可以在脚本末尾使用Wait-Event
调用来无限期地等待永远不会到达的事件,这使您的脚本保持运行,但是 - 与Start-Sleep
不同 -不会阻止通过传递给Register-ObjectEvent
的脚本块处理事件Register-ObjectEvent
的-Action
参数:
$Watcher = New-Object System.IO.FileSystemWatcher
$Watcher.path = "\\192.168.5.127\data\TestWatcher"
$Destination = "C:\TestWatcher"
$Watcher.EnableRaisingEvents = $true
$action = {
$path = $event.SourceEventArgs.FullPath
$name = $event.SourceEventArgs.Name
$changetype = $event.SourceEventArgs.ChangeType
Write-Host "File $name at path $path was $changetype at $(get-date)"
Copy-Item $Watcher.path $Destination
}
# Register the event with a self-chosen name passed to -SourceIdentifier
# and an -Action script block.
$null =
Register-ObjectEvent $Watcher Created -SourceIdentifier FileWatcher -Action $action
# Now wait indefinitely for an event with the same source identifier to arrive.
# NONE will ever arrive, because the events are handled via the -Action script block.
# However, the call will prevent your script from exiting, without
# blocking the processing of events in the -Action script block.
Wait-Event -SourceIdentifier FileWatcher
或者,不用-Action
脚本块并在Wait-Event
循环中处理事件:
$Watcher = New-Object System.IO.FileSystemWatcher
$Watcher.path = "\\192.168.5.127\data\TestWatcher"
$Destination = "C:\TestWatcher"
$Watcher.EnableRaisingEvents = $true
# Register the event with a self-chosen name passed to -SourceIdentifier
# but WITHOUT an -Action script block.
$null = Register-ObjectEvent $Watcher 'Created' -SourceIdentifier FileWatcher
# Now use Wait-Event with the chosen source identifier to
# to indefinitely receive and then process the events as they
# become available.
while ($event = Wait-Event -SourceIdentifier FileWatcher) {
$path = $event.SourceEventArgs.FullPath
$name = $event.SourceEventArgs.Name
$changetype = $event.SourceEventArgs.ChangeType
Write-Host "File $name at path $path was $changetype at $(Get-Date)"
Copy-Item $Watcher.path $Destination
$event | Remove-Event # Note: Events must be manually removed.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.