Register-ObjectEvent cmdlet 在 Powershell 上无法正常工作,但在 ISE 上可以正常工作

Register-ObjectEvent cmdlet not working properly on Powershell but working on ISE

我正在使用 Powershell 脚本来监视文件夹,当创建新项目时,脚本需要将该文件复制到另一个文件夹。

我遇到的问题是,当我在 Powershell ISE 中执行它时,它运行良好,但在 Powershell 上执行时,它仅在 Powershell window 打开的时间段内有效(> 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 call at the end of your script to indefinitely wait for events that will never arrive, which keeps your script running, but - unlike Start-Sleep - does not block processing of events via the script block passed to 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.
}