Retrieving AD Information Based on Username

I have a code to retrieve user information from AD, such as email address, phone number, etc. etc. The codes I'm currently using are:

Set objSysInfo = CreateObject("ADSystemInfo")
strUser = objSysInfo.UserName
msgbox(strUser)
Set objUser = GetObject("LDAP://" & strUser)

It receives current user information. But now I need to analyze the username and get information based on this.

I tried changing objSysinfo.UserName to username and it was empty.

Set objSysInfo = CreateObject("ADSystemInfo")
strUser = "SomeUserName"
msgbox(strUser)
Set objUser = GetObject("LDAP://" & strUser)

How can I get information from AD based on the provided username?

+4
source share
2 answers

URI LDAP . . , "" LDAP:

username = "SomeUserName"

Set rootDSE = GetObject("LDAP://RootDSE")
base  = "<LDAP://" & rootDSE.Get("defaultNamingContext") & ">"
'filter on user objects with the given account name
fltr  = "(&(objectClass=user)(objectCategory=Person)" & _
        "(sAMAccountName=" & username & "))"
'add other attributes according to your requirements
attr  = "distinguishedName,sAMAccountName"
scope = "subtree"

Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADsDSOObject"
conn.Open "Active Directory Provider"

Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandText = base & ";" & fltr & ";" & attr & ";" & scope

Set rs = cmd.Execute
Do Until rs.EOF
  WScript.Echo rs.Fields("distinguishedName").Value
  rs.MoveNext
Loop
rs.Close

conn.Close

, , (ADQuery) .

+11

Ansgar the RootDSE , . , , :

    base  = "<LDAP://" & rootDSE.Get("defaultNamingContext") & ">"

- :

    base  = "<LDAP://" & "DC=corp,DC=foo,DC=com" & ">"

AD corp.foo.com

0

All Articles