【问题标题】:Powershell Script to monitoring status and send email results用于监控状态并发送电子邮件结果的 Powershell 脚本
【发布时间】:2013-07-03 18:18:41
【问题描述】:

我有很多网站来监控它们的启动/关闭状态、可能的错误、ping 以及我设法通过脚本获得的其他内容。我的想法如下:此脚本将与任务调度程序一起运行,获取结果并向我们(来自 SQA 出版物)发送电子邮件。所以,我成功地创建了脚本,他得到了我需要的一切,并在 C: 驱动器中生成了一个 html 文件。我的问题是,在我得到结果后,发送电子邮件的功能没有发送电子邮件。我没有收到任何错误消息,调试很好,SMTP 和所有配置都正确。但它不会发送带有 html 文件的电子邮件!

代码是这样的:

$URLListFile = "C:\URLList.txt"  
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue 
  $Result = @() 


  Foreach($Uri in $URLList) { 
  $time = try{ 
  $request = $null 

  $result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri } 
  $result1.TotalMilliSeconds 
  }  
  catch 
  { 

   $request = $_.Exception.Response 
   $time = -1 
  }   
  $result += [PSCustomObject] @{ 
  Time = Get-Date; 
  Uri = $uri; 
  StatusCode = [int] $request.StatusCode; 
  StatusDescription = $request.StatusDescription; 
  ResponseLength = $request.RawContentLength; 
  TimeTaken =  $time;  
  } 

} 

if($result -ne $null) 
{ 
    $Outputreport = "<HTML><TITLE>Website Report Status</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Website Report Status </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B> Code </B></TD><TD><B> Status </B></TD><TD><B> Duration </B></TD><TD><B> MS (Ping) </B></TD</TR>" 
    Foreach($Entry in $Result) 
    { 
        if($Entry.StatusCode -ne "200") 
        { 
           $Outputreport += "<TR bgcolor=red>" 
        } 
        else 
        { 
            $Outputreport += "<TR>" 
        } 
        $Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>" 
    } 
    $Outputreport += "</Table></BODY></HTML>" 
} 

$Outputreport | out-file C:\URLReport.htm 
Invoke-Expression C:\URLReport.htm   


$EmailFrom = "noreply@domain.com"
$EmailTo = "destinyemail@domain.com"
$EmailSubject = "URL Report"
$emailbody = " body message "
$SMTPServer = "smtpserver.company.com"

$emailattachment = "C:\URLReport.htm"

function send_email {
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($emailfrom)
$mailmessage.To.add($emailto)
$mailmessage.Subject = $emailsubject
$mailmessage.Body = $emailbody

$attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'html')
  $mailmessage.Attachments.Add($attachment)


$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.Send($mailmessage)
}

EDIT4: > ($SmtpServer, 587) 发送“587”我们的 smtp 服务器使用的端口。

【问题讨论】:

  • 您需要说明“不发送”。对于初学者,您不会打电话给send_email,这对发送电子邮件来说有点障碍。有任何错误信息吗?此外,还有一些执行网站监控的产品(有些是免费的,有些不是免费的)。为什么不使用其中之一,而不是自己滚动?
  • 您是否尝试过使用现有的 Send-MailMessage ?
  • 它只是不发送电子邮件,它执行脚本的第一部分,似乎它不会执行“电子邮件”部分。关于我更正的“ send_email ”,缺少脚本的一部分。问题是,我公司不使用其他软件,他们不会为这些软件的许可付费。除此之外,我的老板让我写这个,我用它作为继续学习 powershell 的一种方式。
  • 回答我的人使用了 Send-MailMessage 并且工作得很好,也感谢 Jimbo 的帮助 :)
  • “我公司不使用其他软件,他们不会为该软件的许可付费” - 那么您的公司有一些重大问题。如果可以购买,就不要建造这样的东西(或者更好的是,在某个地方有免费的 - 在这种情况下几乎肯定有),因为最终它可能比使用员工的时间更好,更便宜。但这在这里有点无关紧要,Powershell 是 Windows 的一个组件 - 所以只要您为 Windows 许可证付费,就可以了。

标签: powershell powershell-3.0


【解决方案1】:

由于您使用的是 Powershell v3,因此您应该使用 Send-MailMessage 而不是处理 System.Net

$URLListFile = "C:\URLList.txt"  
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue 
  $Result = @() 


  Foreach($Uri in $URLList) { 
  $time = try{ 
  $request = $null 

  $result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri } 
  $result1.TotalMilliSeconds 
  }  
  catch 
  { 

   $request = $_.Exception.Response 
   $time = -1 
  }   
  $result += [PSCustomObject] @{ 
  Time = Get-Date; 
  Uri = $uri; 
  StatusCode = [int] $request.StatusCode; 
  StatusDescription = $request.StatusDescription; 
  ResponseLength = $request.RawContentLength; 
  TimeTaken =  $time;  
  } 

} 

if($result -ne $null) 
{ 
    $Outputreport = "<HTML><TITLE>Website Report Status</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Website Report Status </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B> Code </B></TD><TD><B> Status </B></TD><TD><B> Duration </B></TD><TD><B> MS (Ping) </B></TD</TR>" 
    Foreach($Entry in $Result) 
    { 
        if($Entry.StatusCode -ne "200") 
        { 
           $Outputreport += "<TR bgcolor=red>" 
        } 
        else 
        { 
            $Outputreport += "<TR>" 
        } 
        $Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>" 
    } 
    $Outputreport += "</Table></BODY></HTML>" 
} 

$Outputreport | out-file C:\URLReport.htm 
Invoke-Item C:\URLReport.htm   

$EmailFrom = "noreply@domain.com"
$EmailTo = "destinyemail@domain.com"
$EmailSubject = "URL Report"
$emailbody = " body message "
$SMTPServer = "smtpserver.company.com"

$emailattachment = "C:\URLReport.htm"

Send-MailMessage -Port 587 -SmtpServer $SMTPServer -From $EmailFrom -To $EmailTo -Attachments $emailattachment -Subject $EmailSubject -Body $emailbody -Bodyashtml;

【讨论】:

  • 效果很好,谢谢。我不知道那个“Send-MailMessage”功能。先生,您对我帮助很大。真的很谢谢你 ! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多