To remove objects from the Active Directory, you must know the object class, relative name of the object, and the distinguished path to the object.
Use the following Visual Basic code as a guide to remove any object from the Active Directory:
Dim RootDSE As IADs Dim Container As IADsContainer Dim RelativePathToObject As String Dim ObjectClass As String Dim ObjectName As String RelativePathToObject = "ou=administrators," ObjectClass = "user" ObjectRelativeName = "cn=TestAdmin" Set RootDSE = GetObject("LDAP://RootDSE") Set Container = GetObject("LDAP://" & RelativePathToObject & RootDSE.Get("DefaultNamingContext")) Call Container.Delete(ObjectClass, ObjectRelativeName)
Using ADSI's IADsDeleteOps interface, you can prune an entire subtree from the directory tree. The IADsDeleteOps interface allows you to remove an object and all its child objects using a single line of code.
Use the following Visual Basic code to delete the Print Queues organizational unit from the base of the tree:
Dim RootDSE As IADs Dim Container As IADsDeleteOps Dim RelativePathToObject As String RelativePathToObject = "ou=Print Queues," Set RootDSE = GetObject("LDAP://RootDSE") Set Container = GetObject("LDAP://" & RelativePathToObject & RootDSE.Get("DefaultNamingContext")) Container.DeleteObject (0)
To rename an object in the Active Directory, you can use the IADsContainer MoveHere method. Although designed for moving objects from one location of the directory to another, when the origin and destination containers are equivalent, and a new relative name for the object is specified, the object is renamed .
To rename any object in the Active Directory, use the following Visual Basic code as a guide:
Dim Container as IADsContainer Dim NewObjectName as IADs Set Container = GetObject("LDAP://ou=Admins,dc=eCommerce2000,dc=com") Set NewObjectName = Container.MoveHere("LDAP://cn=Administrator,ou=Admins, dc=eCommerce2000,dc=com", "cn=DMZAdmin")
Warning
IADsDeleteOps can do significant amounts of damage if improperly executed in the namespace. Be careful when granting rights to users to allow use of this interface. ;
To use the IADsContainer MoveHere method to move an object, bind to the destination container and then specify the object to be moved into the container object as an argument to the MoveHere method, as shown in the following Visual Basic code segment:
Dim Container as IADsContainer Dim NewObjectName as IADs Set Container = GetObject("LDAP://ou=Users,dc=eCommerce2000,dc=com") Set NewObjectName = Container.MoveHere("LDAP://cn=DMZAdmin,ou=Admins,dc=eCommerce2000, dc=com", vbNullString)
Tip
You can move and rename an object simultaneously by passing the new object name as the second argument of the IADsContainer MoveHere method.
Warning
Because VBScript does not have a constant value equivalent to vbNullString, you must explicitly name the relative name of the object (such as cn=DMZAdmin) as the second argument of the IADsContainer MoveHere method .
Top |