簡體   English   中英

Register-ObjectEvent cmdlet 在 Powershell 上無法正常工作,但在 ISE 上工作

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM