Monitor-Website

Monitors a website availability, integrity and response times by looking for a specific string on a page

<#
.SYNOPSIS
	Monitor-Website.ps1 - Monitors a website availability by looking for a specific string on a page
.DESCRIPTION
	Monitor-Website - Website availability, integrity and response times monitoring
.PARAMETER URL
	The URL of the page to monitor.
	Mandatory parameter.
.PARAMETER String
	The string to look for on the page
	Default is null. If not specified, only the availability of the URL is checked.
.PARAMETER Interval
	The sleep interval between each check
	Default is 5 seconds.
.PARAMETER ShowErrors
	Switches the 'ErrorActionPreference' between "Continue" and "SilentlyContinue"
	Default is "no".
.NOTES
	File Name	: Monitor-Website.ps1 
	Author		: Fabrice ZERROUKI - fabricezerrouki@hotmail.com
.EXAMPLE 
	PS D:\> .\Monitor-Website.ps1 -URL www.microsoft.com -String windows -Interval 10
	Check http://www.microsoft.com for "*windows*" on the page every 10 seconds
.EXAMPLE 
	PS D:\> .\Monitor-Website.ps1 -URL www.microsoft.com
	Check http://www.microsoft.com availability every 5 seconds (default value)
	no string looking for, only website availability (default value)
#>
Param(
    [Parameter(Mandatory=$true, HelpMessage="You must provide an URL to test. (www.contoso.com)")]
    [ValidatePattern("(http(s)?://)?([\w-]+\.)+[\w-]+(/[\w- ;,./?%&=]*)?")]
    [string]$URL,
    [string[]]$String,
    $Interval=5,
    [ValidateSet( "yes", "no")]
    [string]$ShowErrors="no"
    )

if ($ShowErrors -eq "yes") {
$global:ErrorActionPreference="Continue"
} else {
$global:ErrorActionPreference="SilentlyContinue"
}
$webClient=New-Object System.Net.WebClient
# Uncomment the next 2 lines if you have to use the IE configured proxy
#$webClient.UseDefaultCredentials=$true
#$webClient.Proxy.Credentials=$webClient.Credentials
$webClient.Headers.Add("user-agent", "PowerShell Script")

if (($URL -like "http://*") -or ($URL -like "https://*")) {
$URLToCheck=$URL
}
else {
$URLToCheck="http://" + $URL
}
$StringToCheck="*" + "$String" + "*"

Write-Host "$URLToCheck" -ForegroundColor DarkGreen;
Write-Host "$StringToCheck" -ForegroundColor DarkGreen;

Write-Host ""
while (1 -eq 1) {
   $output=""
   $startTime=Get-Date
   $output=$webClient.DownloadString("$URLToCheck")
   $endTime=Get-Date
   
   $result=($endTime - $startTime).TotalSeconds

   if (($output -like "$StringToCheck") -and ($output))
	{
	$check="Success`t`t" + $startTime.DateTime + "`t`t" + ($endTime - $startTime).TotalSeconds + " seconds"
	Write-Host "$check"
	}
	else
	{
	$check="Fail`t`t" + $startTime.DateTime + "`t`t" + ($endTime - $startTime).TotalSeconds + " seconds"
	Write-Host "$check"
	Write-Host "String `"$StringToCheck`" not found at $URLToCheck" -ForegroundColor DarkYellow;
	}
   sleep($Interval)
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top