简体   繁体   English

Powershell 和 System.IO.FileSystemWatcher

[英]Powershell and System.IO.FileSystemWatcher

I am new to PowerShell and I am trying to use the System.IO.FileSystemWatcher to monitor the presence of a file in a specified folder.我是 PowerShell 的新手,我正在尝试使用 System.IO.FileSystemWatcher 来监视指定文件夹中文件的存在。 However, as soon as the file is detected I want to stop monitoring this folder immediately and stop the FileSystemWatcher.但是,一旦检测到文件,我想立即停止监视此文件夹并停止 FileSystemWatcher。 The plan is to incorporate the PowerShell script into a SQL Agent to enable users to restore their own databases.该计划是将 PowerShell 脚本合并到 SQL 代理中,以使用户能够恢复自己的数据库。 Basically I need to know the command to stop FileSystemWatcher from monitoring as soon as one file is found.基本上我需要知道一旦找到一个文件就停止 FileSystemWatcher 监视的命令。 Here is the script so far.这是到目前为止的脚本。

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\TriggerBatch"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER A EVENT IS DETECTED
    $action = { $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                Add-content "C:\log2.txt" -value $logline              
              }    

### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY  
    $created = Register-ObjectEvent $watcher Created -Action $action

while ($true) {sleep 1} 

## Unregister-Event Created ??
##Stop-ScheduledTask ??
Unregister-Event $created.Id

This will unregister the event.这将取消注册该事件。 You will probably want to add this to the $action.您可能希望将其添加到 $action。

Do note that if there are events in the queue they will still be fired.请注意,如果队列中有事件,它们仍然会被触发。

This might help too.也可能有帮助。

Scriptblocks that are run as an action on a subscribed event have access to the $Args, $Event, $EventArgs and $EventSubscriber automatic variables .作为订阅事件的操作运行的脚本块可以访问 $Args、$Event、$EventArgs 和 $EventSubscriber 自动变量

Just add the Unregister-Event command to the end of your scriptblock, like so:只需将 Unregister-Event 命令添加到脚本块的末尾,如下所示:

### DEFINE ACTIONS AFTER A EVENT IS DETECTED
$action = { $path = $Event.SourceEventArgs.FullPath
            $changeType = $Event.SourceEventArgs.ChangeType
            $logline = "$(Get-Date), $changeType, $path"
            Add-content "C:\log2.txt" -value $logline  
            Unregister-Event -SubscriptionId $EventSubscriber.SubscriptionId            
          }    

This is the pattern for an event that only performs an action once and then cleans itself up.这是只执行一次操作然后自行清理的事件的模式。

It's difficult to effectively explore these automatic variables since they are within the scope of a Job, but you can futz with them by assigning them to global variables while you are sketching out your code.很难有效地探索这些自动变量,因为它们在 Job 的范围内,但是您可以通过在草绘代码时将它们分配给全局变量来处理它们。 You may also get some joy with Wait-Debugger and Debug-Runspace.您还可以使用 Wait-Debugger 和 Debug-Runspace 获得一些乐趣。 In the case of the $EventSubscriber variable, it returns the exact object you get if you run Get-EventSubscriber (having created a single subscription already).对于 $EventSubscriber 变量,如果您运行 Get-EventSubscriber(已经创建了一个订阅),它会返回您获得的确切对象。 That's how I found the SubscriptionId property.这就是我找到 SubscriptionId 属性的方式。

如果您想停止/取消注册所有已注册的事件,您可以致电

Get-EventSubscriber|Unregister-Event

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM