Powershell 文件监视程序脚本

Powershell 文件监视程序脚本,powershell,Powershell,我正在为两个FTP目录编写一个file watcher Powershell脚本。计划是在创建文件时发送一封电子邮件,这是我的工作。此外,如果在特定时间间隔内未发送任何文件,用户还希望发送警报。 使用register-event命令也可以吗 以下是我到目前为止的情况: #File Watcher 2014 #This script is used for FTP purposes to watch files for a specific directory and alerts when a

我正在为两个FTP目录编写一个file watcher Powershell脚本。计划是在创建文件时发送一封电子邮件,这是我的工作。此外,如果在特定时间间隔内未发送任何文件,用户还希望发送警报。 使用register-event命令也可以吗

以下是我到目前为止的情况:

#File Watcher 2014 
#This script is used for FTP purposes to watch files for a specific directory and alerts when a file is created and to alert when a file has not been sent

$folder = 'C:\TEST' # Enter the root path you want to monitor. 
$filter = '*.txt.gpg'  # You can enter a wildcard filter here. 
# In the following line, you can change 'IncludeSubdirectories to $true if required.                           
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'} 

# Here is the file created event: 

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 

$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$timeStamp = $Event.TimeGenerated 
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green 
Out-File -FilePath C:\TEST\Logs\outlog.txt -Append -InputObject "The file $name was $changeType at $timeStamp"} 
Send-MailMessage -To  -From  -Subject "" -Body   -SmtpServer  

#This part will alert if no files have been found after last Create Time
    for($i=-4,$i -lt 0,$i++){
    $start = (get-date -hour 2).adddays($i);
    $end = (get-date -hour 3).adddays($i);
    {}
Else
Send-MailMessage -To  -From  -Subject  -Body  -SmtpServer 
{}
} 

到底什么对你不起作用?你被困在哪里?您得到了什么(如果有的话)错误?您有一个使用“,”而不是“;”的for循环那不行。你能澄清这个问题吗。如果在(比如)1小时内没有创建任何文件,您是否应该收到警报?或者你的意思是在凌晨1点到2点之间没有生成任何文件时发出警报?我想写一个脚本,如果在某个时间范围内创建了文件,则发送警报;如果在同一时间范围内没有创建任何文件,则发送警报。