Powershell监控网站可用性



我有一个PS脚本,可以监视文本文件中列出的所有网站。截至目前,我正在将其作为作业运行,因此每当任何网站出现故障时,它都会在挂号邮件ID上发出警报,并将HTML文件作为包含所有网站状态的附件。我现在想要的是,它只选择那些关闭并让我发出警报的网站。有人可以在这里帮助我吗?

脚本:-

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

  Foreach($Uri in $URLList) { 
  $time = try{ 
  $request = $null 
   ## Request the URI, and measure how long the response took. 
  $result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri } 
  $result1.TotalMilliseconds 
  }  
  catch 
  { 
   <# If the request generated an exception (i.e.: 500 server 
   error or 404 not found), we can pull the status code from the 
   Exception.Response property #> 
   $request = $_.Exception.Response 
   $time = -1 
  }   
  $result += [PSCustomObject] @{ 
  Time = Get-Date; 
  Uri = $uri; 
  StatusCode = [int] $request.StatusCode; 
  StatusDescription = $request.StatusDescription; 
    TimeTaken =  $time;  
  } 
} 
    #Prepare email body in HTML format 
if($result -ne $null) 
{ 
    $Outputreport = "<HTML><TITLE>Website Availability Report</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Website Availability Report </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B>StatusCode</B></TD><TD><B>StatusDescription</B></TD><TD><B>TimeTaken</B></TD</TR>" 
    Foreach($Entry in $Result) 
    { 
        if($Entry.StatusCode -ne "200") 
        { 
            $Outputreport += "<TR bgcolor=red>" 
             send-mailmessage -to "abc@gmail.com" -from "abc@gmail.com" -subject "Test mail" -SmtpServer XXX@###.com -Attachments "C:UsersDesktopTest.htm"
        } 
        else 
        { 
            $Outputreport += "<TR>" 
        } 
        $Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)<TD align=center>$($Entry.timetaken)</TD></TR>" 
    } 
    $Outputreport | out-file C:UsersDesktopTest.htm 
    $Outputreport += "</Table></BODY></HTML>" 
} 

我可以想到两种方法来做到这一点。首先,我假设您尝试从状态代码不是成功代码的$results捕获每个项目。如果此假设是正确的,那么您已经在代码中拥有了大部分解决方案。以下行:

if($Entry.StatusCode -ne "200") 
{ 
    $Outputreport += "<TR bgcolor=red>" 
     send-mailmessage -to "abc@gmail.com" -from "abc@gmail.com" -subject "Test mail" -SmtpServer XXX@###.com -Attachments "C:UsersDesktopTest.htm"
} 

正在识别这些记录,因此您只需在循环中在此处抓取它们即可。喜欢这个:

if($Entry.StatusCode -ne "200") 
{ 
    $Outputreport += "<TR bgcolor=red>" 
     send-mailmessage -to "abc@gmail.com" -from "abc@gmail.com" -subject "Test mail" -SmtpServer XXX@###.com -Attachments "C:UsersDesktopTest.htm"
 $failedCodeUri += $Entry.uri
} 

在上面的代码片段中,必须声明变量$failedCodeUri,其范围必须在 foreach 循环内外访问。这可以简单地完成,就像在紧靠 foreach 循环开头之前的行上声明变量一样。它还必须是可以使用 += 运算符的类型。这可以通过像这样的简单数组来完成。

$failedCodeUri = @()

当然,如果您需要捕获的不仅仅是 uri,则需要比简单的字符串数组更复杂的数据类型。

另一种可能的解决方案取决于$results对象上可用的"where"方法。如果可用,您可以简单地执行以下操作:

$failedCodeUri = $results.where({$_.StatusCode -ne "200"})

最后一种技术不会在循环中使用。在 foreach 循环之前或之后,您可以使用这行代码返回所有所需的结果。

关于此技术需要注意的几点:在"where"代码块中,正在检查结果集中每个项的条件。使用点运算符使用 $_ 引用当前项,以访问该项存在的属性或方法。

在上面的示例中,将返回整个项及其所有属性。如果你只想要 uri,你可以做这样的事情:

$failedCodeUri = $results.where({$_.StatusCode -ne "200"}).uri