gpt4 book ai didi

Powershell 和 System.IO.FileSystemWatcher

转载 作者:行者123 更新时间:2023-12-01 03:39:36 26 4
gpt4 key购买 nike

我是 PowerShell 的新手,我正在尝试使用 System.IO.FileSystemWatcher 来监视指定文件夹中文件的存在。但是,一旦检测到文件,我想立即停止监视此文件夹并停止 FileSystemWatcher。计划是将 PowerShell 脚本合并到 SQL 代理中,使用户能够恢复自己的数据库。基本上,我需要知道在找到一个文件后立即停止 FileSystemWatcher 监视的命令。这是到目前为止的脚本。

### 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

这将取消注册事件。您可能希望将其添加到 $action 中。

请注意,如果队列中有事件,它们仍将被触发。

This也可能有帮助。

关于Powershell 和 System.IO.FileSystemWatcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31795933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com