Removing Objects from the Active Directory Using Visual Basic

   

Removing Objects from the Active Directory Using Visual Basic

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 & graphics/ccc.gif RootDSE.Get("DefaultNamingContext")) Call Container.Delete(ObjectClass, ObjectRelativeName) 

Deleting an Entire Branch of a Directory Tree Using Visual Basic

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 & graphics/ccc.gif RootDSE.Get("DefaultNamingContext")) Container.DeleteObject (0) 

Renaming Objects in the Active Directory Using Visual Basic

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, graphics/ccc.gif 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. ;


Moving Objects Within a Tree Using Visual Basic

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, graphics/ccc.gif 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


Windows NT. 2000 ADSI Scripting for System Administration
Windows NT/2000 ADSI Scripting for System Administration
ISBN: 1578702194
EAN: 2147483647
Year: 2000
Pages: 194
Authors: Thomas Eck

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net