Did you know that you can use PowerShell to monitor your website and send you alarms when something goes wrong? We had availability issues with our community site and I was quite surprised that the 20-line (!) PowerShell script did the job!
Basically, all I had to do was use the Net.WebClient object and its DownloadString method to query the page (with some proxy handling code I got from Alexey Chuikov), and trap any exception which it generates when something goes wrong. The trap is using our internal relay server to send me and everyone who is involved in the site administration the email.
Here’s the code:
########################################################## # Test-Site - script to test web site availability # and notify in case of any issues # (c) Dmitry Sotnikov # http://dmitrysotnikov.wordpress.com ########################################################## function Test-Site { param($URL) trap{ "Failed. Details: $($_.Exception)" $emailFrom = "my.email@address.com" # Use commas for multiple addresses $emailTo = "my.email@address.com,another.admin@address.com" $subject = "PowerGUI.org down" $body = "PowerGUI web site is down. Details: $($_.Exception)" $smtpServer = "smtp.server.to.use.for.relay" $smtp = new-object Net.Mail.SmtpClient($smtpServer) $smtp.Send($emailFrom, $emailTo, $subject, $body) exit 1 } $webclient = New-Object Net.WebClient # The next 5 lines are required if your network has a proxy server $webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials if($webclient.Proxy -ne $null) { $webclient.Proxy.Credentials = ` [System.Net.CredentialCache]::DefaultNetworkCredentials } # This is the main call $webclient.DownloadString($URL) | Out-Null } Test-Site "http://powergui.org"
To test it you can obviously just put an invalid URL into the call.
Once I had the script running, I just set up a scheduled task in Windows Task Scheduler to run the script every 15 minutes:

One trick I learned from MoW and used in the task, was using the -command parameter (rather than just supplying the script) and including the exit $LASTEXITCODE into the command, so the exit code from the PowerShell script gets registered as the scheduled task result.
So here’s the command-line I have scheduled:
c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Noninteractive -command ". c:\scripts\test-site.ps1; exit $LASTEXITCODE"
Works flawlessly! And can save you tons of money on a monitoring solution. Talk about ROI from learning PowerShell!
Tags: .NET, Examples, Exchange, Internet, OpsMgr, PowerShell, Reporting, email, server management
Subscribe by email




That script looks pretty interesting. The problem I have with local solutions is that I would have to run my Windows box 24/7 and configure it to send me a notification whenever my websites become unavailable.
Yes, this is an obvious limitation. At the same time specifically with the task of monitoring web site availability I actually like that this script is running remotely (actually from another network) so network-related problems such as DNS or routing get caught as well.
Have a look at http://www.mywebkpi.com which does web kpi monitoring around the clock for free. Try the live demo here http://www.mywebkpi.com/cgi-bin/demo-cgi to see how websites can be monitored without scripts and without software or config changes. Talk about ROI here for investing nothing and getting your site monitored.
works great for me
Well I don’t see any limitations :=) Actually, I will be using this for a daily checkout script to report that critical intranet sites are up. The .Proxy and .CredentialCache was driving me crazy and this was a huge help. This ole SysAdmin is gonna have to learn .Net. Thank you very much Dmitry!
Whilst looking for a script to monitor website availability I found another option using kixtart here:
http://www.jjclements.co.uk/index.php/2009/02/19/kixtart-script-to-check-for-website-availability
It seems to also report the actual error codes that may be invoked when there is a webserver issue