Reece
Reece

Reputation: 764

Script to check the status of a URL

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

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

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.

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

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 script:

## 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   

Upvotes: 4

Views: 15503

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

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 isn't needed here.

Upvotes: 6

Related Questions