I want to create a script that detects if there is a new file from with date today in different folders. Sends an email and attach the file to the email.
I have modified a script that check today’s files in a folder
and sends e-mail, but I have not yet successfully attached the file as a copy in the email.
Source: * source: Powershell check files today in a directory - Microsoft Q&A*
I’m happy if the script detects todays file in one folder.
If it’s not possible to attach the file to an e-mail (no e-mail client on server), then I’m happy if the script can attach the name of the file.
Really appreciate your answer.
Here´s the modified script:
$Directories = "E:\Test"
#$OtherDirectory = "E:\Test2"
#$TodaysDirectory = "E:\Test3"
$ThisDay = [datetime]::Today
$OtherFiles = 0
$TodaysFiles = 0
Get-ChildItem $directories |
ForEach-Object{
if ($_.Length -eq 0 -and $_.CreationTime.Date -ne $ThisDay){ # What about non-zero length files?
$OtherFiles++
Copy-Item ...
}
#Kopierar dagens filer till Destination
elseif($_.Length -eq 0 -and $_.CreationTime.Date -eq $ThisDay){ # What about non-zero length files?
$TodaysFiles++
Copy-Item "E:\Test\*.txt" -Destination "E:\Destination"
}
}
if (-not $TodaysFiles){
Clear-Host
$body = "No files are found from today, and $OtherFiles old files were found"
Write-Host $body
Send-MailMessage -To forename.aftername@domain.com -From server@domain.com -Subject "Something wrong" -Body $body -SmtpServer smtp.child.domain.com
}
elseif($OtherFiles){
Clear-Host
$body = "found $OtherFiles old files, and $TodaysFiles current files"
Write-Host $body
Send-MailMessage -To forename.aftername@domain.com -From server@domain.com -Subject "Something wrong" -Body $body -SmtpServer smtp.child.domain.com
}
else{
Clear-Host
$body = "$TodaysFiles are found from today, and $OtherFiles old files were also found"
Write-Host $body
Send-MailMessage -To forename.aftername@domain.com -From server@linkoping.se -Subject "Files from today" -Body $body -SmtpServer smtp.child.domain.com
}