我正在为两个FTP目录编写文件监视器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
{}
}