powershell实现发送邮件方法
task运行方法:
powershell.exe -file c:\path_to_your_script\script.ps1
powershell.exe c:\path_to_your_script\script.ps1
#send mail
#by Hanliang
#date:2024.12.13
$smtpServer = "192.168.*.*"
$smtpPort = 25
$from = "indsysmail@adata.com"
$to = "Hanliang_Li@adata.com"
$date= Get-Date
$subject = "[192.168.*.*]Inda GoCron Hour Exception[$date]"
$body = "The following files were created in the last hour:"
$attPath="D:\MYSQLDBBK\logs\*.*"
# 获取当前日期
# 提取月份
$month = $(Get-Date).Month
$maillog="D:\MYSQLDBBK\maillog_$month.log"
$now = Get-Date
$oneHourAgo = $now.AddHours(-1)
"$(Get-Date)...mail start ....." | Out-File -FilePath $maillog -Append
$files = Get-ChildItem -Path $attPath -Include check*Gocron*.log -File | Where-Object {$_.CreationTime -ge $oneHourAgo }
$checkMail="false"
$cnt=0
$attachments=@();
foreach ($file in
$files) {
$attachments += $($file.FullName)
Write-Host "*******path:[$($attachments)]"
$content = Get-Content -Path $file.FullName
if ($content -eq $null) {
$checkMail="false"
} else {
$checkMail="true"
$cnt++
}
$body += "File: $($file.FullName) `n"
foreach ($line in $content) { $body += " $line `n" } $body += "`n"
}
$attachments | Out-File -FilePath $maillog -Append
if($checkMail -eq "true" -or $cnt -gt 0 ){
Send-MailMessage -From $from -To $to -Subject $subject -Body $body -SmtpServer $smtpServer -Port $smtpPort -Attachments $attachments
#-UseSsl
"$(Get-Date)...mail send pass....."| Out-File -FilePath $maillog -Append
}
"$(Get-Date)...end check mail....."| Out-File -FilePath $maillog -Append
更多推荐


所有评论(0)