Platform SDK: Active Directory, ADSI, and Directory Services

NDS Example Code

To connect to an NDS Server, you bind to the NDS object using an NDS ADsPath. This example shows how to enumerate the various ADSI objects of an NDS server.

Dim dso
Dim obj
Dim usrName
Dim password
Dim serverName
 
servername = "ntmarst2"
userName = "supervisor.ntmarst2"
password = "secretpwd" 
'--bind to the server
Set dso = GetObject("NDS:")
Set cont = dso.OpenDSObject("NDS://" & serverName,userName,password,0) 
'--enumerate the server's objects
For Each obj In cont
    Debug.Print obj.Name & " (" & obj.Class & ")"
Next

To retrieve and modify a user object's attribute, bind to the NDS object and change the desired property value using the Get and Put methods. This example changes a particular user's last name to "Johnson".

Path = "O=NTMARST2/CN=benny"
ADsPath = "NDS://" & serverName & "/" & Path
Set usr = dso.OpenDSObject(ADsPath, userName, password, 0)
Debug.Print usr.Get("Surname")
usr.Put "SurName", "Johnson"
usr.SetInfo

This example creates a new user with a username of "bagheeraw".

Path = "O=NTMARST2"
ADsPath = "NDS://" & serverName & "/" & Path
Set cont = dso.OpenDSObject(ADsPath, userName, password, 0)
Set usr = cont.Create("user", "bagheeraw")
usr.Put "cn", "Bagheera"
usr.Put "Surname", "White"
usr.SetInfo

This example searches an NDS server for a user with a surname of "Bagheera".

ADsPath = "NDS://" & serverName
Set con = CreateObject("ADODB.Connection")
con.Provider = "ADsDSOObject"
con.Properties("User ID") = userName
con.Properties("Password") = password
con.Open "ADSI"
 
Set com = CreateObject("ADODB.Command")
Set com.ActiveConnection = con
com.CommandText = "SELECT ADsPath, 'Object Class' FROM '" & ADsPath & "' WHERE Surname='Bagheera'"
Set rs = com.Execute
 
While Not (rs.EOF)
   Debug.Print rs.Fields("ADsPath")
   rs.MoveNext
Wend