powershell脚本比较两个文本文件和输出应该是一个像电子邮件的通知

时间:2017-12-22 04:46:25

标签: windows powershell

我有一个PowerShell脚本,用于比较两个文本文件并显示输出:

Compare-Object $(Get-Content c:\scripts\x.txt) $(Get-Content c:\scripts\y.txt) -includeequal

但我希望以通知的形式输出,例如电子邮件......

如何将输出转发到电子邮件正文然后发送邮件?

1 个答案:

答案 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-MailMessageGoogle SMTP Config