Sunday, July 3, 2011

Powershell script to monitor multiple SharePoint / IIS Sites

###########################################################################This script helps you in monitoring the availablity of your hosted sites
# and triggers an email for any IIS failures.
# It reads a file and executes the function for each URL's in the URL.txt # file
# written by KannaGanesh 03July11
###########################################################################

function SiteMonitor {
param($URL)
trap{
"Failed.Details: $($URL) $($_.Exception)"
$emailFrom = ""
# Seperate email addresses with comma's
$emailTo = ""
$subject = "$($URL) is un available"
$body = "$($URL) site is down around $endTime

Error Details

$($_.Exception)"
$smtpServer = ""
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}
$webclient = New-Object Net.WebClient
$webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials
}

$startTime = get-date
$endTime = get-date
$webclient.DownloadString($URL) | Out-Null
}
$readfile = Import-Csv D:\MaintenanceScripts\URL.txt
foreach ($line.URL in $readfile){
sitemonitor $line.URL }

Points to Note
1) Ensure you are able to access those sites from the machine where this script will be scheduled
2) Provide the proper file location in $readfile, the URL.txt should be listing all url's under column URL. Something like below.
URL
http://1234.com
http://5678.com
3) This script reports problem based on any IIS errors.

Disadvantage

This script is not supporting for Web Application hosted on Claims Authentication.

Wish you all the best !

No comments:

Post a Comment