2015-07-10 55 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和它的工作:)謝謝! –