继续事件订阅,脚本执行后powershell

Continue event subscription,after script execution powershell

我正在创建一个脚本,以监控下载文件夹中的用户 activity。我决定使用 FileSystemWatcher 事件来触发和警报。

此脚本可以完成工作,但仅当 powershell 实例为 运行 时才有效,您不希望一直有一个 powershell 进程 运行。

有一种方法可以在进程关闭后保留此事件订阅。

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
Register-ObjectEvent $watcher "Created" -Action {
 [System.Windows.Forms.MessageBox]::Show($eventArgs.FullPath) 
}

Marthias 是对的,您可以通过添加计划任务来保持 powershell 进程始终处于活动状态。

启动程序 PowerShell.exe 并添加参数 -noexit -noprofile