Get LDAP Connect connect string when you only have the userid (VBScript)

If you only have the userid and you do not know which OU the userid is located, you can use the following vbscript to get the whole LDAP Connect string.

Define function here.

Public Function getUsersDN(ByVal strUsername )
Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject(“ADODB.Connection”)
Set objCommand = CreateObject(“ADODB.Command”)
objConnection.Provider = “ADsDSOObject”
objConnection.Open “Active Directory Provider”
Set objCommand.ActiveConnection = objConnection

objCommand.Properties(“Page Size”) = 1000
objCommand.Properties(“Searchscope”) = ADS_SCOPE_SUBTREE

objCommand.CommandText = _
“SELECT distinguishedName FROM ‘LDAP://dc=domain,dc=com’ ” & _
“WHERE objectCategory=’user’ ” & _
“AND sAMAccountName='” & strUsername & “‘”
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strDN = objRecordSet.Fields(“distinguishedName”).Value
getUsersDN = strDN
objRecordSet.MoveNext
Loop
End Function

Here’s how you use the function.

strUserID = inputbox (“Enter your userid”)
struser = getUsersDN(strUserID)
Set objUser = GetObject(“LDAP://” & strUser)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s