我有一个PowerShell脚本,用于比较两个文本文件并显示输出:
Compare-Object $(Get-Content c:\scripts\x.txt) $(Get-Content c:\scripts\y.txt) -includeequal
但我希望以通知的形式输出,例如电子邮件......
如何将输出转发到电子邮件正文然后发送邮件?
答案 0 :(得分:1)
要将输出比较放入电子邮件并通过Gmail发送,您可以使用Send-MailMessage命令,如下所示:
$From = "YourEmail@gmail.com"
$To = "ToMail@Domain.com"
$Cc = "CCMail@Domain.com"
$Subject = "String Comparison"
$comparison = (Compare-Object (Get-Content c:\scripts\x.txt) (Get-Content c:\scripts\y.txt) -includeequal).InputObject
foreach($line in $comparison)
{
$Body+= $line
}
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential (Get-Credential)
有关详细信息,请查看Send-MailMessage和Google SMTP Config