Using PowerShell to monitor a web server
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.