Retrieve Lync Connection Information
Submitted by Tracy A. Cerise, University of Kentucky
This script pulls Microsoft Lync 2010 connection information, such as the number of connections per Front End Server and number of unique users, and either displays it to the console or writes it to a file, depending on the parameters you supply with the call.
To use this script, copy the code shown below, paste it into a text editor (like Notepad) and then save the file with a .PS1 extension (for example, C:\Scripts\Connections.ps1). After that, you can run the script from within the Lync Server Management Shell simply by typing the full path to the .PS1 file and then pressing ENTER:
C:\Scripts\Connections.ps1
For a full description as well as requirements for connecting to the Front End Server database, see the introductory comments in the script below. For additional help on using this script, type the following at the Lync Server Management Shell prompt and press ENTER:
C:\Scripts\Connections.ps1 -help
#################################################################################################
# Connections.ps1
#
# Program to pull Lync connection information.
# This program will pull complete information across all
# frontend servers in a pool. It can also be used to find
# specific connection information on an individual user by
# supplying the user's sip address. The parameter for
# the pool to access for connection information can be
# pre-populated so it doesn't have to be passed on the command line.
#
# ACKNOWLEDGEMENT: This program's database connection information
# was originally taken from the "List Connections
# to Registrar Pools" submitted by Scott Stubberfield
# and Nick Smith from Microsoft to the Lync 2010
# PowerShell blog
# (https://blogs.technet.com/b/csps/) on June 10, 2010.
#
# NOTE: In order to gain remote access to each frontend server's
# RTCLOCAL database where connection information is found,
# you need to open the local firewall for port 1434.
# Also, need to go into the SQL Server Configuration Manager
# and for RTCLOCAL, enable named pipes and restart the SQL
# service for the named pipes to take effect.
#
# Port 1434 is required in order to make the connection to
# the named instance RTCLOCAL on the remote machines.
#
#
# Written by: Tracy A. Cerise (tracy@uky.edu)
# Date: March 2011
#
# Modification History
# --------------------
#
# Could add # users with enterprise voice enabled and other
# individual stats to this page as well
#
#################################################################################################
##########################################################
# Commandline Parameters to use for running this program #
##########################################################
param($Pool = "your.pool.here", $SIPAddr, $FilePath, [switch] $Help)
#################################################################################################
################################# Functions #########################################
#################################################################################################
function GetData {
param ($sipAddr = $null, $server)
##############################################################################################
# Went to using a named parameter for this function due to the
# way Powershell does its thing with parameter passing, which
# is NOT GOOD! At any rate, need to call this function
# as you would from a command line: GetData -sipAddr "value"
# -server "value"
#
# Also, assuming a value of NULL for the SIP address of an
# individual user, mostly to use this for finding overall
# values, only occasionally to seek specific users.
##############################################################################################
if ($sipAddr) {
$whereClause = "where R.UserAtHost = '$sipAddr' "
}
else {
$whereClause = $null
}
#Define SQL Connection String
$connstring = "server=$server\rtclocal;database=rtcdyn;trusted_connection=true;"
#Define SQL Command
$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = "Select (cast (RE.ClientApp as varchar (100))) as ClientVersion, R.UserAtHost as UserName, Reg.Fqdn `
From rtcdyn.dbo.RegistrarEndpoint RE `
Inner Join `
rtc.dbo.Resource R on R.ResourceId = RE.OwnerId `
Inner Join `
rtcdyn.dbo.Registrar Reg on Reg.RegistrarId = RE.PrimaryRegistrarClusterId `
$whereClause `
Order By ClientVersion, UserName "
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connstring
$connection.Open()
$command.Connection = $connection
$sqladapter = New-Object System.Data.SqlClient.SqlDataAdapter
$sqladapter.SelectCommand = $command
$results = New-Object System.Data.Dataset
$recordcount=$sqladapter.Fill($results)
$connection.Close()
return $Results.Tables[0]
}
#################################################################################################
function Help {
"
NAME
Connections.ps1
SYNOPSIS
Returns current connection count for all frontend servers in a given pool
including a breakdown of connection by client, frontend server and users.
It can also be used to return connection information on an individual user.
SYNTAX
Connections.ps1 [-Pool <PoolFQDN>] [-SIPAddr] [-FilePath] [-help]
DESCRIPTION
This program will return a connection count for a given pool. The program
can be edited to set a default pool. You will also be able to get
information on an individual user by providing the users SamAccountName.
EXAMPLES
-------------------------- EXAMPLE 1 --------------------------
C:\PS>connections.ps1
Description
-----------
Returns information on all connections on all frontend servers in the
default pool that has been hard-coded into the program.
-------------------------- EXAMPLE 2 --------------------------
C:\PS>connections.ps1 -Pool alternate.pool.fqdn
Description
-----------
Returns information on all connections on all frontend servers in the
pool given with the pool parameter.
-------------------------- EXAMPLE 3 --------------------------
C:\PS>connections.ps1 -SIPAddr userid@sip.domain
Description
-----------
Returns all connection information for the given user including which
frontend server connected to, how many connections and which clients
connected with.
-------------------------- EXAMPLE 4 --------------------------
C:\PS>connections.ps1 -filepath c:\path\to\file\filename.csv
Description
-----------
Returns information on all connections on all frontend servers in the
default pool that has been hard-coded into the program and in addition
writes out all the raw connection information into the filename specified.
"
exit
}
#################################################################################################
#################################################################################################
################################# Main Program #########################################
#################################################################################################
#############################################
### Main program
#############################################
# If the help switch is toggled
if ($help) {
Help
}
#
# Here is where we pull all the frontend server(s) from our topology for the designated
# pool and iterate through them to get current connections from all the servers.
#
# There are three possibilities here:
# 1. Have collection of frontend servers
# 2. Have a single frontend server or
# 3. Have no servers
#
# Only need to check for the first two cases, the third is implied otherwise...
$feServers = Get-CsComputer -Pool $Pool
if ($feServers.count) {
# Frontend pool collection, iterate through them
for ($i=0; $i -lt $feServers.count; $i++) {
if ($SIPAddr) {
$data = GetData -sipAddr $SIPAddr -server $feServers[$i].identity
}
else {
$data = GetData -server $feServers[$i].identity
}
# Since an individual user's connections are all homed on one server, won't have
# data coming back from all frontend servers in the case of searching for a
# single user
if ($data) {
$overallrecords = $overallrecords + $data
}
}
}
elseif ($feServers) {
# Have a standalone server or a FE pool of only one server
if ($SIPAddr) {
$data = GetData -sipAddr $SIPAddr -server $feServers.identity
}
else {
$data = GetData -server $feServers.identity
}
# Make sure we have data to work with...
if ($data) {
$overallrecords = $data
}
}
# Check to see if we have any data to act on
if (! $overallrecords) {
write-host -ForegroundColor Yellow "`r`nNothing returned from query!`r`n"
# Nothing else to do
exit
}
else {
$count=0
$userHash = @{}
$clientHash = @{}
$serverHash = @{}
$overallrecords | foreach-object {
# Each record has three components: Connected Client Version, User's SIP
# address and the frontend server's FQDN. Here, we'll build a hash
# for each of these components for each record.
# Build hash of users
if (! $userHash.ContainsKey($_.UserName)) {
$userHash.add($_.UserName, 1)
}
else {
$userHash.set_item($_.UserName, ($userHash.get_item($_.UserName) + 1))
}
# Build hash of servers
if (! $serverHash.ContainsKey($_.fqdn)) {
$serverHash.add($_.fqdn, 1)
}
else {
$serverHash.set_item($_.fqdn, ($serverHash.get_item($_.fqdn) + 1))
}
# Build hash of clients
# Lets get rid of the extraneous verbage from the client version names, if applicable
if ($_.ClientVersion.contains('(')) {
# Get rid of extraneous verbage
$clientName = $_.ClientVersion.substring(0, $_.ClientVersion.IndexOf('('))
}
else {
# Have a client name with no extraneous verbage
$clientName = $_.ClientVersion
}
if (! $clientHash.ContainsKey($clientName)) {
$clientHash.add($clientName, 1)
}
else {
$clientHash.set_item($ClientName, ($clientHash.get_item($ClientName) + 1))
}
$count++
}
}
################################
#### Output Query Results ####
################################
# If output to file is chosen, then write out the results and a note to that effect
# then exit
if ($FilePath) {
$overallrecords | Export-Csv $FilePath
write-host -foregroundcolor green "`r`nQuery Results written to $FilePath`r`n"
exit
}
write-host -foregroundcolor cyan "`r`nClient Version/Agent Connections"
write-host -foregroundcolor cyan "--------------------------------------------------"
foreach ($key in $clientHash.keys) {
# Break down client version into its two component parts and print
# them out along with their respective counts in a nice format
$index = $key.indexof(" ")
if ($index -eq "-1") {
# No second part
$first = $key
$second = " "
}
else {
# Client version/agent has to main parts
$first = $key.substring(0, $index)
$second = $key.substring($index + 1)
}
$value = $clientHash.$key
"{0, -20} {1, -20} {2, 5}" -f $first, $second, $value
}
write-host -foregroundcolor cyan "--------------------------------------------------"
write-host -foregroundcolor cyan "`r`n`r`nFrontend Server Connections"
write-host -foregroundcolor cyan "-------------------------------"
foreach ($key in $serverHash.keys) {
$value = $serverHash.$key
"{0, -22} {1, 5}" -f $key, $value
}
write-host -foregroundcolor cyan "-------------------------------"
"{0, -22} {1, 5}" -f "Total connections...", $count
"`r`n"
write-host -foregroundcolor cyan "Total Unique Users/Clients"
write-host -foregroundcolor cyan "-------------------------------"
"{0, -22} {1, 5}" -f "Users...............", $userHash.count
"{0, -22} {1, 5}" -f "Client Versions.....", $clientHash.count
write-host -foregroundcolor cyan "-------------------------------"
"`r`n"
Write-Host -ForegroundColor Green "Query complete`r`n"
Comments
Anonymous
January 01, 2003
i added to that listing user aliases Thanks alot for the GREAT work blogs.technet.com/.../list-connections-and-users-connected-to-lync-registrar-pool.aspxAnonymous
August 01, 2011
Nice Job!Anonymous
November 07, 2011
Hmm, I just get "Nothing returned from query!" when I try and run the script.... Anybody else get that? MikeyAnonymous
May 24, 2014
Pingback from Script: Get-CsConnections.ps1 - See User Connections, Client Versions, Load Balancing in Lync Server - Ehlo World!Anonymous
June 10, 2014
Pingback from Script: Get-CsConnections.ps1 - See User Connections, Client Versions, Load Balancing in Lync Server - Ehlo World!