如何在Powershell脚本中仅一次从FileSystemWatcher触发操作

时间:2019-04-10 18:09:31

标签: powershell scripting filesystemwatcher

我正在使用FileSystemWatcher类创建监视文件夹的脚本,并在以下文件夹事件上执行应用程序:创建新文件还是已写入文件。即使脚本有效,我也有一个问题。当filesystemwatcher触发该动作时,该动作会发生几次并运行我的exe应用程序服务时间。我如何确保该操作仅对每个filesystemewatcher触发一次。

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS 
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Dt\CA\PD"
$watcher.Filter = "*.xlsx"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true  
$watcher.NotifyFilter =[IO.NotifyFilters]'LastWrite'

### DECIDE WHICH EVENTS SHOULD BE WATCHED 
$null=Register-ObjectEvent $watcher  Created -Action $action
$null=Register-ObjectEvent $watcher Changed -Action $action

while($true){
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = { &Start-Process  "C:\Users\sammy\Documents\parsing Engine 1.0.exe"}
      }

0 个答案:

没有答案