gygon

PowerShell Ping Test

Posted by gygon on Wednesday, November 16th, 2011

Used this little script the other day for a basic network test to an access point I was testing. It just pings the device every 10 seconds (or whatever you set it at) and only outputs when there is an error and the time it occurred. Made for an easy way to leave this running over […]

continue reading

Using PowerShell to monitor a web server

Posted by gygon on Monday, November 14th, 2011

A couple years ago I posted about a PowerShell script that would read in a web page. I used that to setup a basic web site test to go through a list of web sites/servers and make sure they were up and running and email an alert if they were down or the size of […]

continue reading

Monitoring the Event Log with PowerShell

Posted by gygon on Saturday, March 21st, 2009

One of my goals with learning more about PowerShell is to be able to monitor the event logs on servers and notify me via email when certain events happen. The system I’m looking to monitor are not part of a domain, are in remote locations on isolated networks. Some of the main things I’m looking […]

continue reading

Using PowerShell to monitor a web server

Posted by gygon on Monday, November 14th, 2011

A couple years ago I posted about a PowerShell script that would read in a web page. I used that to setup a basic web site test to go through a list of web sites/servers and make sure they were up and running and email an alert if they were down or the size of the returned page had changed indicating something had changed. It came up recently and figured it would be easier to post here for future reference.

This can definitely be cleaned up and made more versatile but it does what I need it to do and the only changes I’ve made over the last two years has been to adjust the size checks of the websites as changes were made.

#-----------------------------------------------------------
#
# WebSiteCheck.ps1
#
# Checks websites to ensure they are within the size range for the specified page.
# If site size is out of range, email alert is sent.
#
# Usage: ./WebSiteCheck.ps1 (-debug)
#   ex:  ./websitecheck.ps1
#   ex:  ./websitecheck.ps1 -debug
#
#-----------------------------------------------------------

#------------------------------------------------------
Param([switch]$debug)

if ($debug) { $DebugPreference = " continue" }
write-debug "Starting website check now..."
#------------------------------------------------------

function Get-Web($url,
    [switch]$self,
    $credential,
    $toFile,
    [switch]$bytes)
{
    #.Synopsis
    #    Downloads a file from the web
    #.Description
    #    Uses System.Net.Webclient (not the browser) to download data
    #    from the web.
    #.Parameter self
    #    Uses the default credentials when downloading that page (for downloading intranet pages)
    #.Parameter credential
    #    The credentials to use to download the web data
    #.Parameter url
    #    The page to download (e.g. www.msn.com)
    #.Parameter toFile
    #    The file to save the web data to
    #.Parameter bytes
    #    Download the data as bytes
    #.Example
    #    # Downloads www.live.com and outputs it as a string
    #    Get-Web http://www.live.com/
    #.Example
    #    # Downloads www.live.com and saves it to a file
    #    Get-Web http://wwww.msn.com/ -toFile www.msn.com.html
    $webclient = New-Object Net.Webclient
    if ($credential) {
        $webClient.Credential = $credential
    }
    if ($self) {
        $webClient.UseDefaultCredentials = $true
    }
    if ($toFile) {
        if (-not "$toFile".Contains(":")) {
            $toFile = Join-Path $pwd $toFile
        }
        $webClient.DownloadFile($url, $toFile)
    } else {
        if ($bytes) {
            $webClient.DownloadData($url)
        } else {
            $webClient.DownloadString($url)
        }
    }
}

function SendEmail($SendTo,$SendSubject,$SendMessage)
{
	$SmtpClient = new-object system.net.mail.smtpClient
	$MailMessage = New-Object system.net.mail.mailmessage
	$SmtpClient.Host = "SMTP.YOURDOMAIN.COM"
	$FromAddress = new-object System.Net.Mail.MailAddress("MyEmail@YOURDOMAIN.com", "YOUR NAME")
	$mailmessage.sender = $FromAddress
	$mailmessage.from = $FromAddress
	$mailmessage.To.add($SendTo)

	$mailmessage.Subject = $SendSubject
	$mailmessage.IsBodyHtml = 1

	$mailmessage.Body = $SendMessage

	$Credentials = new-object System.Net.networkCredential
	$Credentials.UserName = "USERNAME@YOURDOMAIN.com"
	$Credentials.Password = "YOURPASSWORD"
	$SMTPClient.Credentials = $Credentials
	$SMTPClient.Port = 25
	$smtpclient.Send($mailmessage)
}

# Set who the email goes to and what the subject will be.
$sendtoemail = 'support@YOURDOMAIN.com'
$BaseSubject = 'Website Check: '

# Uncomment the line below to output the retrieved web page to the screen
# write $webpagecall.length
# write $webpagecall

########### Site 1 ##################
write-debug 'Checking Site 1'
$webpagecall = Get-Web 'https://www.site1.com/subfolder/ASP/pagetocheck.asp'
write-debug $webpagecall.length
if (($webpagecall.length -lt 15948) -or ($webpagecall.length -gt 15948)) {
   write-debug 'out of range'
   write-debug $webpagecall.length
   $SendSubject = $BaseSubject + 'SITE 1'
   SendEmail $sendtoemail $SendSubject $webpagecall
 }

########### Site 2 ##################
write-debug 'Checking Site 2'
$webpagecall = Get-Web 'https://www.site2.com'
write-debug $webpagecall.length
if (($webpagecall.length -lt 12428) -or ($webpagecall.length -gt 13455)) {
   write-debug 'out of range'
   write-debug $webpagecall.length
   $SendSubject = $BaseSubject + 'SITE 2'
   SendEmail $sendtoemail $SendSubject $webpagecall
 }

########### Site 3 ##################
write-debug 'Checking Site 3'
$webpagecall = Get-Web 'https://www.site3.com'
write-debug $webpagecall.length
if (($webpagecall.length -lt 11000) -or ($webpagecall.length -gt 11005)) {
   write-debug 'out of range'
   write-debug $webpagecall.length
   $SendSubject = $BaseSubject + 'SITE 3'
   SendEmail $sendtoemail $SendSubject $webpagecall
 }

write-debug 'Finished running checks.'

A few notes:

  • Be sure to change the email credentials in the SendEmail function.
  • The Site 1, 2, 3 can be reduced to a single site or have however many sites you want to check added on. Just edit the info for the site you want to check.
  • I left all the size checks as a range as some of the sites I check vary slightly (ex: current date/time) so this allows a range for them to still pass the check and not throw false alarms.
The way I use the script is on a server that has it scheduled to run every 10 minutes. This way if a site goes down I find out pretty quick.

Posted in: Featured, PowerShell, Tech.

Comments are closed.