Powershell脚本无法发送电子邮件警报

时间:2017-08-08 09:57:58

标签: powershell powershell-v2.0 powershell-v4.0

当我的加载过程失败时,我没有收到电子邮件警报,下面是我的脚本。请让我知道我在哪里做错了,并建议一种使条件有效的方法

$body = Get-Content -Path D:\APPDATA\FDMEE\Scripts\output.txt | Out-String 
$fail = Get-Content "D:\APPDATA\FDMEE\Scripts\output.txt" | Select-String "FAILED" 
if($fail-eq "$True"){Send-MailMessage -From "sharihar@xpioc.com" -To ""sharihar@xpioc.com""-Subject " Load failed on the Production Server." -Body "$body"  -Attachments "D:\APPDATA\FDMEE\Scripts\output.txt" -smtpServer mail.xpioc.com}

Output.txt内容

process_id       : 50836
STATUS           : FAILED
RULE_ID          : 16
RULE_NAME        : INTERNATIONAL

1 个答案:

答案 0 :(得分:3)

改变这个:

$fail = Get-Content "D:\APPDATA\FDMEE\Scripts\output.txt" | Select-String "FAILED" 
if($fail-eq "$True")

对此:

$fail = Get-Content "D:\APPDATA\FDMEE\Scripts\output.txt" | Select-String "FAILED" -Quiet
if($fail)

您的代码正在测试$fail变量是否包含不匹配的字符串“True”。

根据LotPings注释,如果将-Quiet开关添加到Select-String,则会得到真/假结果。然后,您可以测试$fail以确认真/假结果是否为真。