Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OpenDSObject("LDAP://domain",u,p,...) and get employeeID attribute

Status
Not open for further replies.

JustinEzequiel

Programmer
Jul 30, 2001
1,192
PH
From thread222-1194741, I got code to authenticate a user.
Code:
Set dso = GetObject("LDAP:")
set ob1 = dso.OpenDSObject("LDAP://domain",
                            strUserName,strPassword,
           ADS_SECURE_AUTHENTICATION + ADS_SERVER_BIND)
This works just fine.

Now they want me to retrieve the employeeID/employeeNumber attributes of the authenticated user.
How do I get at these attributes.
My Google-fu seems weak this week.
(Perhaps I'm not using the right keywords.)
 
update:
found the following which works for getting the employeeID and employeeNumber attributes

Code:
Set objDomain = GetObject ("GC://rootDSE")
objADsPath = objDomain.Get("defaultNamingContext")
Set objDomain = Nothing
Set con = CreateObject("ADODB.Connection")
con.provider ="ADsDSOObject"
con.open "Active Directory Provider"
Set Com = CreateObject("ADODB.Command")
Set Com.ActiveConnection = con
Com.CommandText ="select samAccountName, employeeID,employeeNumber from 'GC://"+objADsPath+"' WHERE objectCategory='person' and name='Ezequiel, Justin'"
Set rs = Com.Execute

The only limitation is this needs to run on a machine that is joined to the domain.
Is there a way to get this working on a machine not joined to the domain?

(BTW, my laptop is not joined to the domain which was why I thought the above solution and others do not work.)
 
yet another update:

the following works from a machine outside the domain,
drawback is you need to know the full path to the user's container.
is there an easier way?
Should I move this thread to forum774 (LDAP Forum)?

Code:
ADS_SECURE_AUTHENTICATION = 1
ADS_SERVER_BIND = 512

u = "myuser"
p = "******"
 
Set dso = GetObject("LDAP:")

Set o = dso.OpenDSObject("LDAP://mydomain.com/OU=IT Development,OU=Global IT,OU=City,OU=Support,OU=Country,OU=Managed Objects,DC=mydomain,DC=com", u, p, ADS_SECURE_AUTHENTICATION + ADS_SERVER_BIND)

dim si, an

on error resume next

for each si in o
    an = si.Get("samAccountName")
    if an=u then
        wscript.echo "Name:", si.Get("Name")
        wscript.echo "samAccountName:", an
        wscript.echo "employeeID:", si.Get("employeeID")
        exit for
    end if
next
 
update:

much simpler, I just need to know four constants.

I'll post more updates here unless I'm already irritating you guys.

Code:
option explicit
 
const LDAPHOST = "myldaphost"
const LDAPPORT = 389
const DEFAULTNAMINGCONTEXT = "DC=mydomain,DC=com"
const DOMAIN = "mydomain.com"
 
dim user, passwd
dim con, com, rs, fld
dim cs, q
 
user = "myuser"
passwd = "******"
 
cs = "Provider=ADSDSOObject;User Id="+ user +"@"+ DOMAIN +";Password="+ passwd +";"
q = "select samAccountName, employeeID, Name from 'LDAP://"+ LDAPHOST + ":" + cstr(LDAPPORT) +"/"+ DEFAULTNAMINGCONTEXT +"' WHERE objectCategory='person' and samAccountName='"+ user +"'"
 
Set con = CreateObject("ADODB.Connection")
con.provider = "ADsDSOObject"
con.open cs
Set com = CreateObject("ADODB.Command")
Set com.ActiveConnection = con
com.CommandText = q
Set rs = Com.Execute
While not rs.eof
    for each fld in rs.Fields
        wscript.echo fld.Name & ": " & rs.fields(fld.Name) 
    next
    rs.movenext
Wend
rs.close
set rs=nothing
con.close
set con=nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top