To create an object in the directory, you must first create an object of a specific class and then assign values to the mandatory attributes of the object before writing it to the directory. From this procedure, one can quickly conclude that the only thing differentiating one object from another within the directory is the class of the object and the attributes values assigned to each object.
Armed with this knowledge, you can easily enumerate, create, and remove objects in the directory using generic code syntax, and change the attribute assignments as appropriate for each object created.
To enumerate all user objects in a particular container in the Active Directory, use a variation of the following Visual Basic code:
Dim RootDSE As IADs Dim UserContainer As IADsContainer Dim User As IADs Dim RelativePathFromDomainToUserContainer As String RelativePathFromDomainToUserContainer = "ou=user accounts," Set RootDSE = GetObject("LDAP://RootDSE") Set UserContainer = GetObject("LDAP://" & RelativePathFromDomainToUserContainer & RootDSE.Get("DefaultNamingContext")) UserContainer.Filter = Array("User") For Each User In UserContainer Debug.Print User.AdsPath Next
Note
In the previous example you are using the IADsContainer Filter property to display objects belonging only to the User class.
By changing the IADsContainer Filter property value assignment (and potentially , the relative path to the groups you want to enumerate) you can display all groups defined in a particular container.
An example of such a procedure is shown in the following Visual Basic code segment:
Dim RootDSE As IADs Dim GroupContainer As IADsContainer Dim Group As IADs Dim RelativePathFromDomainToGroupContainer As String RelativePathFromDomainToGroupContainer = "ou=Groups,ou=Chicago," Set RootDSE = GetObject("LDAP://RootDSE") Set GroupContainer = GetObject("LDAP://" & RelativePathFromDomainToGroupContainer & RootDSE.Get("defaultNamingContext")) GroupContainer.Filter = Array("Group") For Each Group In GroupContainer Debug.Print Group.AdsPath Next
To display all computer accounts in a particular container, use the following Visual Basic code:
Dim RootDSE As IADs Dim ComputerAccountContainer As IADsContainer Dim ComputerAccount As IADs Dim RelativePathFromDomainToComputerContainer As String RelativePathFromDomainToComputerContainer = "ou=Workstations,ou=Computer Accounts, ou=Chicago" Set RootDSE = GetObject("LDAP://RootDSE") Set ComputerAccountContainer = GetObject("LDAP://"& RelativePathFromDomainToComputerContainer & RootDSE.Get("defaultNamingContext")) ComputerAccountContainer.Filter = Array("Computer") For Each ComputerAccount In ComputerAccountContainer Debug.Print ComputerAccount.AdsPath Next
Top |