2015-07-10 54 views
0

我的脚本设置为检查超过2天的文件的目录,然后发送电子邮件给组提交。如果在2天内提交的目录中有文件,则表示感谢。但是,当有多个文件存在时,这个foreach会用多个邮件来杀死我。有没有办法只发送一封电子邮件,无论文件数量多少?Powershell脚本为每个文件发送电子邮件,只需要一封电子邮件

$path = "C:\testing\Claims" 
Foreach($file in (Get-ChildItem $path *.txt -Recurse)) 
{ 
    If($file.LastWriteTime -lt (Get-Date).adddays(-2).date) 
    { 
    Send-MailMessage ` 
    -From [email protected] ` 
    -To [email protected]` 
    -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 protected] ` 
    -To [email protected]` 
    -Subject "File Received" ` 
    -Body "We have received your file for this week. Thank you!" ` 
    -SmtpServer smtp.smtp.com 
    } 
    } 

回答

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 [email protected] ` 
    -To [email protected]` 
    -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 protected] ` 
    -To [email protected]` 
    -Subject "File Received" ` 
    -Body "We have received your file for this week. Thank you!" ` 
    -SmtpServer smtp.smtp.com 
} 
+0

不得不改变$ twoDaysAgo =(获取最新).DddDays (-2).Date到$ twoDaysAgo =(Get-Date).addDays(-2).Date和它的工作:)谢谢! –