Powershell/.NET AD Search Request for Lync objects without using AD Module
This is more of an aide-memoir than anything else, but if you need to get user details, including Microsoft Lync SIP Address and Home Server and you can’t use Powershell Active Directory Module, then these are the Attribute Names you need to use to query the Lync Extended Attributes using the System.DirectoryServices.Protocols.SearchRequest class.
$Request = New-Object System.DirectoryServices.Protocols.SearchRequest | |
$Request.DistinguishedName = $RootContainer | |
$Request.Filter = $SearchFilter | |
$Request.Scope = "Subtree" | |
$Request.Attributes.Add("distinguishedName") | Out-Null | |
$Request.Attributes.Add("objectclass") | Out-Null | |
$Request.Attributes.Add("mail") | Out-Null | |
$Request.Attributes.Add("objectguid") | Out-Null | |
$Request.Attributes.Add("msrtcsip-primaryhomeserver") | Out-Null | |
$Request.Attributes.Add("msrtcsip-primaryuseraddress") | Out-Null | |
$Response = $LDAPConnection.SendRequest($Request) |
Function Get-UserPrincipal($userName) | |
{ | |
$dsam = "System.DirectoryServices.AccountManagement" | |
$rtn = [reflection.assembly]::LoadWithPartialName($dsam) | |
$cType = "domain" #context type | |
$iType = "DistinguishedName" | |
$dsamUserPrincipal = "$dsam.userPrincipal" -as [type] | |
$principalContext = new-object "$dsam.PrincipalContext"($cType,$RootDomain) | |
Write-Host Looking for DN $UserName in Container $principalContext.Name | |
$a = $dsamUserPrincipal::FindByIdentity($principalContext,$iType,$userName) | |
Write-Host Result: $a | |
return $a | |
} # end Get-UserPrincipal |
and was getting the information I need back using this:
Function Populate-UserObject($UserArg) | |
{ | |
$thisUser = new-Object($t + ".User") | |
$thisUser.Name = $UserArg.Name | |
$thisUser.Email = $UserArg.EmailAddress | |
$thisUser.ID = $UserArg.Guid | |
$innerObject = $UserArg.GetUnderlyingObject() | |
$thisUser.SIP = $innerObject."msRTCSIP-PrimaryUserAddress" | |
return $thisUser | |
} |
but it wasn’t working when I added msRTCSIP-PrimaryUserAddress  into the Attributes list. I found out that if you left out the Attributes.Add entries completely, and didn’t have any, you got back all the Attributes. That meant I could scan the list, and find out that what I should be looking for was msrtcsip-primaryuseraddress – turns out it’s case-sensitive.