Help needed in reporting in script for website monitoring

by Sravan at 2013-04-24 03:45:22

I m trying to prepare a script for website monitoring, which takes a list of URLs from a text file and displays the status in a HTML output, however one of the output items, “timeTaken” is displaying the result but it is including all the previous results, attached is the screenshot (Error.jpg).


I tried several things couldn’t fix it… Can anyone help me how do i fix this, below is my code…

##############################################################################
## Test-Url
##############################################################################

## The URI to test
$URLListFile = “D:\URLList.txt”
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
$Result = @()

Foreach($Uri in $URLList) {
$time = @()
$time += try{
clear-variable -name time
$request = $null
$result += $null
## Request the URI, and measure how long the response took.
$result += Measure-Command { $request = Invoke-WebRequest -Uri $uri }
$result.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;
}

#$result
}

#Prepare email body in HTML format
if($result -ne $null)
{
$EmailBody = “<HTML><TITLE>Ericsson Website Availability Report</TITLE><BODY><H2> Website Availability Report </H2><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”)
{
$EmailBody += “<TR bgcolor=red>”
}
else
{
$EmailBody += “<TR>”
}
$EmailBody += “<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>”
}
$EmailBody += “</Table></BODY></HTML>”
}

$EmailBody | out-file C:\Scripts\Test.htm
Invoke-Expression C]
by Sravan at 2013-04-24 04:31:25
I got this problem fixed, thanks anyway :slight_smile:
by happysysadm at 2013-04-24 06:01:41
Hi, just out of curiosity, how did you solve this? By adding ‘clear-variable -name time’ maybe?