检查 URL 状态的脚本 [英] Script to check the status of a URL

查看:61
本文介绍了检查 URL 状态的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到过一些与此类似的问题,但没有找到适合我的情况的问题.

I have seen a few questions similar to this but have not found one that will work for my situation.

我有一个存储在文本文件中的 URL 列表,我需要运行该列表以查看它们是否返回 404 错误.我正在使用 powershell 并且一直在使用这里的示例:http://gallery.technet.microsoft.com/scriptcenter/Powershell-Script-for-13a551b3#content

I have a list of URL's stored in a text file that I need to run through to see if they return a 404 error. I am using powershell and have been using the example here: http://gallery.technet.microsoft.com/scriptcenter/Powershell-Script-for-13a551b3#content

我目前正在测试一个 Confluence 页面的链接,在 Chrome 中查看控制台我可以看到返回的第一个状态是 404 - Not Found,然后是 304、200 之后的十几个请求.

I am currently testing a link to a confluence page, watching the console in Chrome I can see the first status returned is a 404 - Not Found, then a dozen or so requests afterwards that are 304, 200.

我猜测第一个 404 之后的请求会影响我的结果,我需要脚本根据第一个响应返回.

I am guessing the requests after the first 404 are affecting my results, I need the script to return based on that first response.

到目前为止,我已经尝试过 powershell、php 和 javascript 解决方案,但都没有成功.

I have tried powershell, php and javascript solutions with no luck so far.

总而言之,有没有一种方法可以仅根据第一个响应返回答案?

So all in all is there a way to return an answer based on the first response alone?

脚本:

## The URI list to test 
$URLListFile = "H:\xxx\xxx\urlList.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; 
  ResponseLength = $request.RawContentLength; 
  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>ResponseLength</B></TD><TD><B>TimeTaken</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 H:\xxx\xxx\test.htm 
Invoke-Expression H:\xxx\xxx\test.htm   

推荐答案

如果你想让你的脚本在第一个错误后退出循环,你可以尝试这样的事情:

If you want your script to exit from the loop after the first error, you could try something like this:

Foreach($Uri in $URLList) {
  $error.Clear()

  $time = Measure-Command { $request = Invoke-WebRequest -Uri $uri } 2>$null

  if ($error.Count -eq 0) {
    $time.TotalMilliseconds
  } else {
    $error[0].Exception.Response
    break
  }
}

此处不需要 AFAICS try..catch.

AFAICS try..catch isn't needed here.

这篇关于检查 URL 状态的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆