application pools monitoring using PS

by mayank at 2013-04-05 10:48:15

Hi,

I am working on a master script to get the status of the application pools,websites,services.

Need your help on this.Here what I want is to get the status of the Application pool and alert me using SMTP if any pool is down.

Right now I have the script below which tests the websites using a websites.txt file and updates me on email if a website is down.

Can you guys get me the script for the application pools monitoring using below script ??

##########################################################
# Test-Site - script to test web site availability
# and notify in case of any issues
#
#
##########################################################

function Test-Site {
param($URL)
trap{
"Failed. Details: $($.Exception)"
$emailFrom = "WebSiteMon@mercer.com"
# Use commas for multiple addresses
$emailTo = "gourav.keshwani@mercer.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)
}
$webclient = New-Object Net.WebClient
# This is the main call
$webclient.DownloadString($URL) | Out-Null
}

$websitesD = (Get-Content websitesD.txt)

foreach($website in $websitesD)
{
Test-Site "$website"
Write-host Test-Site "$website"
}
by DonJ at 2013-04-06 06:26:28
As a note, you can use the CODE button to format your code. Makes it easier to follow.
by DonJ at 2013-04-06 06:27:54
I’m curious - is there a specific need to monitor the app pool? If the Web site is responding to HTTP, then the app pool is up. If the app pool is down, the Web site doesn’t work. Wouldn’t it be easiest to just do what you’re doing and attempt to connect to the Web site?
by JasonHelmick at 2013-04-06 08:32:49
I would agree with Don on this. Why not use something like Invoke-WebRequest to check the status

Invoke-WebRequest -Uri http://www.whatever.com | Select-Object -Property StatusCode

If you want to check the App pool, what your really looking for is the status of the W3WP.exe process. If no process is running for that pool, then the website is not being used - however it doesn’t mean that there is a problem. Here’s a line that will check all the W3WP process on a web server, but I think you would be better off going down the path of the Invoke-WebRequest.

PS> Get-WmiObject win32_process -filter ‘name="w3wp.exe"’ | Select-Object Name, ProcessId, @{n=‘AppPool’;e={$_.GetOwner().user}}

Cheers!

Jason

Hi Mayank,
I have to setup a similar alert in my SP environment “Whenever any SP site is down we should get email alerts”, I tried script you have provided but it is not working.
Could you please explain a little bit the process to set it up. where to put website url…??I am new to Powershell. please help…