我的脚本设置为检查目录中是否有超过2天的文件,然后将电子邮件发送给要提交的组。如果目录中的文件在2天内提交,则表示感谢。然而,当有多个文件时,foreach正在用多封电子邮件杀了我。有没有办法只发送一封电子邮件而不管文件数量是多少?
$path = "C:\testing\Claims"
Foreach($file in (Get-ChildItem $path *.txt -Recurse))
{
If($file.LastWriteTime -lt (Get-Date).adddays(-2).date)
{
Send-MailMessage `
-From emailer@email.com `
-To me@email.com`
-Subject "File Not Received" `
-Body "Your claims files for this week not available. Please submit them ASAP so that processing can begin." `
-SmtpServer smtp.smtp.com
}
Else
{
Send-MailMessage `
-From email@email.com `
-To me@email.com`
-Subject "File Received" `
-Body "We have received your file for this week. Thank you!" `
-SmtpServer smtp.smtp.com
}
}
答案 0 :(得分:2)
只需检查过去2天内提交的文件数量,然后根据该数字发送邮件:
$path = "C:\testing\Claims"
$twoDaysAgo = (Get-Date).AddDays(-2).Date
$submitted = Get-ChildItem $path *.txt -Recurse |
? { $_.LastWriteTime -ge $twoDaysAgo }
if (@($submitted).Count -eq 0) {
Send-MailMessage `
-From emailer@email.com `
-To me@email.com`
-Subject "File Not Received" `
-Body "Your claims files for this week not available. Please submit them ASAP so that processing can begin." `
-SmtpServer smtp.smtp.com
} else {
Send-MailMessage `
-From email@email.com `
-To me@email.com`
-Subject "File Received" `
-Body "We have received your file for this week. Thank you!" `
-SmtpServer smtp.smtp.com
}