简体   繁体   English

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

[英]Continue event subscription,after script execution powershell

I am creating a script, to monitor user activity in the downloads folder.我正在创建一个脚本,以监视下载文件夹中的用户活动。 Ive decided to use FileSystemWatcher event to trigger and alert.我决定使用 FileSystemWatcher 事件来触发和警报。

This script do the job, but only works when the instance of powershell is running, y dont want to have a powershell process running all the time.这个脚本可以完成这项工作,但只有在 powershell 实例运行时才有效,你不想让 powershell 进程一直运行。

There is a way to to persist this event subscription after the process is closed.有一种方法可以在进程关闭后保留此事件订阅。

$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 is right you can have a schedule task to keep powershell process always alive by adding. Marthias 是对的,您可以通过添加计划任务来保持 powershell 进程始终处于活动状态。

on start a program PowerShell.exe and add arguments -noexit -noprofile在启动程序PowerShell.exe并添加参数-noexit -noprofile

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

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