Geeks With Blogs

Lance's TextBox

Here is a PowerScript function to automate the process of getting/logging the current number of connections to a particular Web Server.  It will tell you the number of connections for each site on the server, so you can see the number of SharePoint connections, SharePoint Central Admin connections, etc.  Thanks to MOW and Lee Holmes for their examples of using the PerformanceCounter class in PowerShell.

function Get-WebServiceConnections()
$results = @{}
$perfmon = new-object System.Diagnostics.PerformanceCounter
$perfmon.CategoryName = "Web Service"
$perfmon.CounterName = "Current Connections"

$cat = new-object System.Diagnostics.PerformanceCounterCategory("Web Service")
$instances = $cat.GetInstanceNames()

foreach ($instance in $instances)
$perfmon.InstanceName = $instance
$results.Add($instance, $perfmon.NextValue())
write-output $results

This will give me output like so:

PS C:\> Get-WebServiceConnections

Name Value
---- -----
_Total 12
SharePoint Central Administ... 1
SharePoint - 80 9
Default Web Site 2

PS C:\> $conns = Get-WebServiceConnections
PS C:\> $conns["SharePoint - 80"]
PS C:\>


You could set the CounterName to any of the other Web Service counters that are available, to do things like measure incoming and/or outgoing bytes per second, the number of incoming HTTP requests per second, etc.  To see a list of all available counters, use the GetCounters() method of the PerformanceCounterCategory ($cat in the example above, ie $cat.GetCounters(“SharePoint – 80”)).

Also, to get more information, you can use $perfmon.NextSample() instead of .NextValue().  NextSample() will return the enture CounterSample object, including RawValue and BaseValue (used to determine the value returned by NextValue()), TimeStamp, etc.

Posted on Wednesday, June 3, 2009 10:29 AM PowerShell | Back to top

Comments on this post: Get the number of current SharePoint connections

# re: Get the number of current SharePoint connections
Requesting Gravatar...
When I run Get-WebServiceConnections SERVERNAME I only get back the default web site.

Running local on the web server I get all sites.

Any ideas why this is?

Left by Mark on Nov 08, 2012 2:27 PM

Your comment:
 (will show your gravatar)

Copyright © Lance Robinson | Powered by: