Powershell: seeking file in a folder and send by smtp relay mail
Views (176)
the title of this post explain all. Useful if you have to sent last lofg error file and so on
insert in a .ps1 file and schedule with task scheduler of
$dir = “”
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.Fullname
$attachment = $latest.Fullname
if($attachment)
{
$emailSmtpServer = “”
$emailSmtpServerPort = “”
$emailSmtpUser = “”
$emailSmtpPass = “”
$EmailFrom = “”
$EmailTo = @(“”, “second mail recipient”)
$EmailBody = “”
$EmailSubject = “”
try
{
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -body $EmailBody -SmtpServer $emailSmtpServer -Attachments $attachment
}
catch
{
}
start-sleep -s 20
Remove-Item $attachment
}

This was originally posted here.
*This post is locked for comments