【发布时间】:2017-08-06 22:54:35
【问题描述】:
我想在 Windows 2012 Server 启动时在后台运行一个 PowerShell 脚本,该脚本执行以下操作:
- 监视文件夹和所有子文件夹(不是计划任务;需要“实时”)
- 调用防病毒扫描(如果有人熟悉赛门铁克,我想使用
DoScan.exe) - 如果文件“干净”,则将文件复制到目标文件夹;隔离那些没有的人
- 记录所有活动
我已经找到了一些代码片段并为此构建了一个框架,但我遇到了各种问题。首先,PowerShell 脚本在运行不到 10 秒后退出。然而,在 ISE 中连续运行。其次,-WindowStyle Hidden 没有像我希望的那样运行。我不希望用户直接知道脚本正在运行。第三(这可能更像是赛门铁克的事情),我的“调用扫描”例程似乎注册为 1 个大扫描,而不是单个文件扫描。
感谢任何指导。请解释为什么/为什么不这样做,以及我是否应该使用另一种语言,以及是否应该将其分解为多个脚本。
$incomingfile = "E:\"
$filter = "*.*"
$watcher = New-Object IO.FileSystemWatcher $incomingfile, $filter -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters] 'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $watcher Created -SourceIdentifier FileCreated -Action {
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$changeType = $Even.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
foreach ($incomingfile in $watcher) {
& "C:\Program Files (x86)\Symantec\Symantec Endpoint Protection\DoScan.exe" /ScanFile $path
}
}
【问题讨论】:
标签: powershell background move startup