Search code examples
powershelllync-2013

Get Current User URI for Lync 2013 via PowerShell


I'm am trying to get the current user's URI that is signed into Lync on the machine the PS script is run on. I've Googled to no avail. One method I thought of trying was to get the Windows logged in name and then parse that into an e-mail address but there will be instances in which this won't give the correct URI. Is this achievable?


Solution

  • Assuming I understand your question...

    1) Getting sip-address for current user using the ActiveDirectory-module.

    (Get-ADUser $env:USERNAME -Properties msRTCSIP-PrimaryUserAddress).'msRTCSIP-PrimaryUserAddress'
    

    2) Getting sip-address for current user using DirectorySearcher.

    $filter = "(&(objectCategory=User)(SamAccountName=$env:USERNAME))"
    $property = 'msRTCSIP-PrimaryUserAddress'
    
    $domain = New-Object System.DirectoryServices.DirectoryEntry
    $Searcher = New-Object System.DirectoryServices.DirectorySearcher
    $Searcher.SearchRoot = $domain
    $Searcher.PageSize = 1000
    $Searcher.Filter = $Filter
    $Searcher.SearchScope = "Subtree"
    $Searcher.PropertiesToLoad.Add($property) | Out-Null
    
    # Value
    ($Searcher.FindAll()).Properties[$property]