繁体   English   中英

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

[英]Continue event subscription,after script execution powershell

我正在创建一个脚本,以监视下载文件夹中的用户活动。 我决定使用 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

暂无
暂无

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

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