Get SharePoint Health Score using PowerShell

If you look at the HTTP headers SharePoint 2010 adds to a response you might notice the one that is called “X-SharePointHealthScore”. This special header is used in conjunction with HTTP throttling and ranges in value from 0 to 10.

HeaderFiddler

By default this score is calculated using two performance counters: Memory/Available Mbytes and ASP.NET/Requests Current. A value “0” indicates everything is fine, while a value of “10” indicates SharePoint will start throttling requests (if enabled). There is quite some info around explaining the workings and configuration. This blog post for instance: http://blogs.msdn.com/b/besidethepoint/archive/2010/09/13/http-request-throttling-in-sharepoint-2010.aspx (by Josh Gavant).

Since this header is added to every response it can be used for different purposes, from monitoring to load balancing. I wanted to create a small “tool” to report the score without having to use a network monitor or HTTP sniffer like Fiddler. And what better way than to use PowerShell for this task?

The script uses a simple PowerShell function with a System.Net.WebClient object connecting to a SharePoint site and retrieving the header:

function ReadHealthScore()
{
	param([string]$url,[System.Net.NetworkCredential]$cred=$null)
	
	$wc = New-Object Net.WebClient
	
	# Include the IE9 User Agent
	$wc.Headers.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;)")

	if($cred -eq $null)
	{
		$cred = [System.Net.CredentialCache]::DefaultCredentials;
	}
	$wc.credentials = $cred;
	$page = $wc.DownloadString($url);
	
	# This header holds the Health Score
	return $wc.ResponseHeaders.Item("X-SharePointHealthScore")
}

Combined with some logic to calculate average, minimum and maximum:

# Wait in number of seconds
$wait = 1
$times = 5
$score = @()

# Set the URL to a page to avoid redirection errors
$url = "http://portal.demo.loc/Pages/Default.aspx"

for ($i=1; $i -lt $times+1; $i++) {
 	$score = $score + (ReadHealthScore($url))
	Write-Progress -Activity "Reading HealthScore" -PercentComplete (($i/$times)*100) -Status "Working"
  	Start-Sleep -Seconds $wait
}

$calc = $score | Measure-Object -Average -Maximum -Minimum
Write-Host (Get-Date -f o) "Average:"$calc.Average " High:"$calc.Maximum " Low:"$calc.Minimum

We get the following output:

HealthScore1

HealthScore2

With a little modification you can output to xml or CSV. Schedule it using Task Scheduler and you will have a nice little health reporter.

Download the script here.

Note: since this script doesn’t use any specific SharePoint cmdlets, you can use standard Windows PowerShell.

Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s