Monthly Archives: May 2013

Powershell to check if an application pool has stopped

I had an oddball situation where the app pool of a given site would keep stopping. No one visits this site with a browser, it’s used just for search crawling. So when it goes down, we never know.

In theory SCOM can tell us this, but SCOM produces so much noise that it’s easy to miss things.

import-module webadministration

	function main{
	#get-command -module webadministration will show all the IIS stuff
	$appPoolName = "SharePoint - www.mysite.com443"
	
	$dt = get-date
	$ComputerName = $env:computername
	If((get-WebAppPoolState -name $appPoolName).Value -eq "Stopped")
	{
		write-host "Failure detected, attempting to start it"
		start-webAppPool -name $appPoolName
		start-sleep -s 60
		
		If((get-WebAppPoolState -name $appPoolName).Value -eq "Stopped")
		{
			write-host "Tried to restart, but it didn't work!"
			sendmail "AppPoolRestart Failed" "App Pool $appPoolName restart on $ComputerName failed - this will effect search `n $dt"
			#log to event log
		}
		else
		{
			write-host "Looks like the app pool restarted ok"
			$subjectString = "AppPool Restart was needed"
			$body = "A routine check of the App Pool $appPoolName on $ComputerName found that it was not running, it has been started. `n $dt"
			sendmail $subjectString $body
			#log to event log?
		}
	}
	else
	 {
	 write-host "app pool $appPoolName is running"
	 }
 } #end main function
 
 function sendmail($subject, $body)
 {
    
    write-host "in Sendmail with subject: $subject, and body: $body"
	
	$EmailFrom = "WEBMONITOR@mydomain.com"
	$EmailTo = "jack@mydomain.com"
	$EmailBody = $body
	$EmailSubject = $subject
	$SMTPServer = "DNS.name.of.your.internal.smtp.server.com"
 
	Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $subject -body $EmailBody -SmtpServer $SMTPServer
	"Emailed $subject on $dt to $EmailTo" | out-file -filepath "CheckSearchAppPools.log" -append 
 }
 
 #call main function
 main

PowerShell Cheat Sheet V2.00

CheatSheetP1

For years I’ve had this handy PowerShell Cheat Sheet at my desk, the original came from:  http://blogs.msdn.com/b/powershell/archive/2007/01/25/powershell-cheat-sheet-redux-the-pdf-version.aspx

I’ve looked a few times for an update, but never found one so I created my own.

I emailed the original author, Ben Pearce from Microsoft to ask his permission to post my updated version online. Ben replied it was ok:

Hi Jack

Thanks for sending me this.  It’s really nice to hear that you found the cheat sheet useful.  I’ve stopped working so closely with PowerShell now and actually manage a team of Premier Field Engineers.  So, I haven’t updated the sheet and I`m not aware of new version circulating.

Thanks for taking the time to update it yourself and feel free to post this on your blog.
Thanks

Ben

That said here’s the updated version – it adds a 3rd page with a few extra tips and there is a bit thrown in about $true and $false on page 1.

PowerShell Cheat Sheet V2.00

I created a new 3 page cheat sheet for SharePoint to supplement the above sheet:

SharePoint PowerShell Cheat Sheet

If you want both in the same file they can be found here:

Combined PowerShell and SharePoint Cheat Sheet

I’ve introduced co-workers to PowerShell and the Cheat Sheet is always very popular:

CheatSheetwall