我有一个脚本,它检查几个文件夹中是否有新文件,如果有新文件的话,它会发送一封包含日志文件的电子邮件。
$source = Get-Content "C:\AAA\source.txt"
$logDate = get-date -format yyyy-MM-dd
$logtime = get-date -format HH:mm
$log = "C:\AAA\$logdate.log"
$mailkomu = "[email protected]"
$mailod = "[email protected]"
$subject = "New files in DDD"
$zprava = "Message"
ForEach($path in $source){
Test-path $path
Write-Output "$logtime - $path - ok" | Out-File $log -Append
If ((Get-ChildItem -Path $path -Force | Measure-Object).Count -eq 0) {
Write-Output "no files" | Out-File $log -Append
}
Else {
Write-Output "new files" | Out-File $log -Append
$data = Get-ChildItem -Path $path -Recurse | Select-Object DirectoryName, Name
$data | Out-File $log -Append
Send-MailMessage -From $mailod -To $mailkomu -Subject $subject -Body $zprava -SmtpServer $SMTPServer -Attachments $log
}
}
source.txt中有4个不同的路径,但可以有更多的路径。计数并不重要。
检查新文件非常有效。
问题是,如果第二条路径上有新文件,它会立即发送电子邮件,而不会等待其他路径被检查。
Example:
first path - no file - sends no email
second path - new file - sends email with log only for the first and the second path
third path - new file - sends email with log only for the first, second and the third path
fourth path - no file - sends no email
有没有一种方法可以让它检查所有路径,如果有新文件,它会在检查所有路径后只发送一封带有日志的电子邮件?