I've long been using the following awesome function posted in an FAQ by K0b3. This works great when the user is in the same domain as you. But not when they are not.
I've just started working at a government job for a county in Arizona. They have several forests and each forest has several domains. My user ID has access rights throughout the enterprise. I tried editing the above code as follows by hard coding the LDAP path, but I don't get any results returned.
My user ID is located under DC=root,DC=county,DC=gov.
Anyone have any idea why this would fail or how to get it to work? Ideally I would prefer to not have to hard code at all and have the entire enterprise searched no matter what forest or domain the user exists in.
Any help is greatly appreciated.
Code:
Public Function SearchDistinguishedName(ByVal vSAN)
' Function: SearchDistinguishedName
' Description: Searches the DistinguishedName for a given SamAccountName
' Parameters: ByVal vSAN - The SamAccountName to search
' Returns: The DistinguishedName Name
Dim oRootDSE, oConnection, oCommand, oRecordSet
Set oRootDSE = GetObject("LDAP://rootDSE")
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
Set oCommand = CreateObject("ADODB.Command")
oCommand.ActiveConnection = oConnection
oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
">;(&(objectCategory=User)(samAccountName=" & vSAN & "));distinguishedName;subtree"
Set oRecordSet = oCommand.Execute
On Error Resume Next
SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")
On Error GoTo 0
oConnection.Close
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing
Set oRootDSE = Nothing
End Function
I've just started working at a government job for a county in Arizona. They have several forests and each forest has several domains. My user ID has access rights throughout the enterprise. I tried editing the above code as follows by hard coding the LDAP path, but I don't get any results returned.
Code:
Public Function SearchDistinguishedName(ByVal vSAN)
Dim oConnection, oCommand, oRecordSet
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
Set oCommand = CreateObject("ADODB.Command")
oCommand.ActiveConnection = oConnection
oCommand.CommandText = "<LDAP://DC=public_defender,DC=county,DC=gov" & _
">;(&(objectCategory=User)(samAccountName=" & vSAN & "));distinguishedName;subtree"
Set oRecordSet = oCommand.Execute
On Error Resume Next
SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")
On Error GoTo 0
oConnection.Close
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing
End Function
My user ID is located under DC=root,DC=county,DC=gov.
Anyone have any idea why this would fail or how to get it to work? Ideally I would prefer to not have to hard code at all and have the entire enterprise searched no matter what forest or domain the user exists in.
Any help is greatly appreciated.