Welcome to EMC Consulting Blogs Sign in | Join | Help

roberto.ortega

Pinging a SharePoint Site with a Script

Background

Keeping a SharePoint site running 24/7 has its challenges. There are several levels of hardware and software issues that must be inline so that everything works properly. One way to avoid prolonged downtime in the event of an issue is by constant script monitoring of your site. This script should be run at frequent intervals of just a few minutes and should be run from a server that is not the same as the SharePoint server, so that notifications can be sent if the SharePoint server is dead.

The Solution

I wrote a simple PowerShell script that will take parameters for the URL and site name. It does not use the SharePoint API so that it may be run on any server with PowerShell. The script will pull the specified URL from the server and will only send an email if the page does not succeed.

Additional Notes

I found that in my environment, due to intermittent database issues while backups where taking place, we would get a message indicating 'Cannot connect'. This issue was deemed trivial so I set up the script to ignore it unless it happens twice in 20 seconds. Depending on your environment this may not be acceptable.

The Code

##########################################################
# Test-Site - script to test web site availability
# and notify in case of any issues
# Roberto Ortega (EMC)
# Keeps a log of each time the server is warmed up, which
# is when the script get a 'Cannot connect...' response
# from the server once, but not after a second try.
##########################################################

function Test-Site {
    param($URL, $siteName)
	try {
	    $webclient = Get-Webclient
	    $response = $webclient.DownloadString($URL)
		#check for 'Cannot connect to the configuration database'
		if ($response.StartsWith("Cannot connect")){
			# give the server a chance to re-establish the database connection
			Start-Sleep -s 20
			
			# try again to get the web page
			$response = $webclient.DownloadString($URL)
			if ($response.StartsWith("Cannot connect")){
				throw New-Object System.ApplicationException("The server did not connect to the database after two tries.")
			}else{
				echo ((date -f "yyyy-MM-dd HH:mm:ss") + "`t" + $URL + "`t" + $siteName) >> c:\log\WarmupLog.csv
			}
		}
	}
	catch{
        "Failed. Details: $($_.Exception)"
        $emailFrom = "webmasters@example.com"
        # Use commas for multiple addresses
        $emailTo = "roberto.ortega@example.com"
        $subject = "Web site is down ($siteName)"
        $body = "$subject"
		$body = $body + "`nError: $($_.Exception)"
		$body = $body + "`n`nThis message was generated on server '" + $Env:computername
		$body = $body + "' via PowerShell script '" + $MyInvocation.InvocationName + "'"
        $smtpServer = "smtp.example.com"
        $smtp = new-object Net.Mail.SmtpClient($smtpServer)
        $smtp.Send($emailFrom, $emailTo, $subject, $body)
        exit 1
    }

} 

function Get-Webclient {    
	$wc = New-Object Net.WebClient
	$wc.UseDefaultCredentials = $true
	$wc.proxy = $null
	$wc}


Test-Site args[0] args[1]
Published 29 October 2011 13:36 by roberto.ortega

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Personal Edition), by Telligent Systems