Monday, March 5, 2018

This Script will take the list of URLs from a text tile( in this case from D:\URLList.txt), and results output with URL, StatusCode, Description. Response Length and the time taken fields in a HTML format report  

 - You can run this script in a schedule task to run at regular intervals for continuous monitoring

############################################################################## 
## 
## Website Availability Monitoring 
## Created for XXX account
## Date : xx xx xxxx
## Version : 1.0 
## Email: abcd@xyz.com 
############################################################################## 
 
 
## The URI list to test 
$URLListFile = "C:\Temp\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 C:\Temp\Test.htm 
Invoke-Expression C:\Temp\Test.htm