我正在使用
New-Object System.IO.FileSystemWatcher
/ Register-ObjectEvent
(Powershell) 来监视文件夹中的新文件并执行操作。它作为服务用户帐户下的计划任务运行,并且作为“操作”的一部分,它执行 Python 脚本。
我最近更改了 Python 脚本,我可以在触发“事件”时观察到新行为,但是,在我的日志中(由 Python 脚本记录)我仍然看到我已经删除的日志行。就好像我的文件系统观察器有一个“幽灵”或“缓存”实例在运行。
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "\\ab\b\c"
$watcher.Filter = "*.*"
$watcher.EnableRaisingEvents = $true
$action =
{
# call python script to do all the work
$pythonPath = 'C:\a\b\c.py args'
Start-Process "C:\Program Files (x86)\Python3\python.exe" -ArgumentList @($pythonPath) -NoNewWindow -PassThru -Wait -RedirectStandardError psstderr.txt -RedirectStandardOutput psstout.txt
}
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) { Start-Sleep 5 }
我试过
Get-EventSubscriber
和 Get-FileSystemWatcher
但他们没有给我任何东西,因为我不在同一个会话中。我尝试重新启动服务器,但这也没有帮助。
现在我想这可能是因为我没有使用 Stop-Process 关闭进程 - 但是当我使用 Get-Process 看到正在运行的进程时,我没有看到 Python 正在运行。
TLDR;在发生事件时,文件系统观察器执行机器上不再存在的 python 脚本的“缓存”版本。
我在编辑任务时通过更改任务计划程序中的“开始于”位置解决了这个问题。当我将“开始于”指定为我的脚本文件夹(我的文件观察器 ps 所在的位置)时,我不再看到“ghost/cached”日志。