Powershell - Lync Map a SIP User to the IP Address it came from

i need testers on this one.... please try it if you want and let me know the results....

basically it will discover what sip users are registered against the particular FE server and report on its client version / sip address and the originating IP address..

can be helpful if you want to identify multiple  clients and where they are logging in from...

 

$computername = $env:COMPUTERNAME
$domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$strdomain = $domain.name
$lyncserver = $computername + "." + $strdomain
Import-Module lync

$SqlServer = $lyncserver + "\RTCLOCAL"
$SqlCatalog = "RTCDYN"
$SqlQuery = "select * from RegistrarEndpoint"
 
#*************** Query Above *******************************************************************************
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SqlServer; Database = $SqlCatalog; Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet

$SqlAdapter.Fill($DataSet)
 
$SqlConnection.Close()
#Clear

$tempstore = $dataset.tables[0].rows

foreach ($t in $tempstore)
{
 if ($t.isserversource -ne "False")
 {
        $bytearray0 = $t.clientapp
        $bytearray1 = $t.contactinfo
 $bytearray2 = $t.sipheaderfrom
 $EncodingType = "System.Text.ASCIIEncoding"
 $Encode = new-object $EncodingType
 $clientapp = $Encode.GetString($ByteArray0)
        $contactinfo = $encode.getstring($bytearray1)
 $sipheaderfrom = $encode.getstring($bytearray2)
 
 $c = $contactinfo.split(';')
 $t = $c[0].split(':')
 $clientip = $t[1]
 write-host "SIP USER   : `t`t" $sipheaderfrom
   write-host "Client IP  : `t`t" $clientip    
 write-host "Client App : `t`t" $clientapp 
        
 }
}