2013-07-03 89 views
3

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

的代码是这样的:

$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 = "[email protected]" 
$EmailTo = "[email protected]" 
$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)Beeing我们的SMTP服务器使用 “587” 的端口。

+0

您需要说明“不发送”。对于初学者来说,你不会调用send_email函数,这对发送电子邮件来说有点阻碍。任何错误消息?此外,还有一些产品(一些免费,一些不免费)执行网站监控。为什么不使用其中的一种,而不是自己滚动? – alroc

+0

您是否尝试过使用现有的Send-MailMessage? – Jimbo

+0

它只是不会发送电子邮件,它执行脚本的第一部分,它似乎不会执行“电子邮件”部分。关于我更正的“send_email”,缺少脚本的一部分。事情是,我的公司不使用其他软件,他们不会支付这些软件的许可证。除此之外,我的老板让我写这篇文章,并用它来保持学习PowerShell。 – InLoveWithPs1

回答

8

由于您使用的是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 = "[email protected]" 
$EmailTo = "[email protected]" 
$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; 
+0

完美的工作,谢谢。我不知道那个“Send-MailMessage”函数。先生,你帮了我很多。真的很感谢你 ! :) – InLoveWithPs1