Recipe15.9.Creating an Object


Recipe 15.9. Creating an Object

Problem

You want to create an object in Active Directory.

Solution

In each solution below, an example of adding a user object named jsmith is shown. Modify the examples as needed to include whatever class and attributes you need to create.

Using a graphical user interface

  1. Open ADSI Edit.

  2. If an entry for the naming context you want to browse is not already displayed, do the following:

  3. Right-click on ADSI Edit in the right pane and click Connect to....

  4. Fill in the information for the naming context, container, or OU you want to add an object to. Click on the Advanced button if you need to enter alternate credentials.

  5. In the left pane, browse to the container or OU you want to add the object to. Once you've found the parent container, right-click on it and select New

    Under Select a Class, select user.

  6. For the cn, enter jsmith and click Next.

  7. For sAMAccountName, enter jsmith and click Next.

  8. Click the More Attributes button to enter additional attributes and values.

  9. Click Finish.

Using a command-line interface

Create an LDIF file called create_object.ldf with the following contents:

dn: cn=jsmith,cn=users,dc=rallencorp,dc=com changetype: add objectClass: user samaccountname: jsmith

Then run the following command:

> ldifde -v -i -f create_object.ldf

It is also worth noting that you can add a limited number of object types with the dsadd command. Run dsadd /? from a command line for more details.

Using VBScript
set objUsersCont = GetObject(LDAP://cn=users,dc=rallencorp,dc=com") set objUser = objUsersCont.Create("user", "CN=jsmith") objUser.Put "sAMAccountName", "jsmith" ' mandatory attribute in W2K objUser.SetInfo

Discussion

To create an object in Active Directory, you have to specify the objectClass, relative distinguished name (RDN) value, and any other mandatory attributes that are not automatically set by Active Directory. Some of the automatically generated attributes include objectGUID, instanceType, and objectCategory.

In the jsmith example, the objectclass was user, the RDN value was jsmith, and the only other mandatory attribute that had to be set was sAMAccountName. Admittedly, this user object is unusable in its current state because it will be disabled by default and no password is set, but it should give you an idea of how to create an object.

Using a graphical user interface

Other tools such as AD Users and Computers could be used to do the same thing, but ADSI Edit is useful as a generic object editor.

One attribute that you will not be able to set via ADSI Edit is the password (unicodePwd attribute). It is stored in binary form and cannot be edited directly. If you want to set the password for a user through a GUI, you can do it with the AD Users and Computers snap-in.

Using a command-line interface

For more on ldifde, see Recipe 15.15.

With dsadd you can set numerous attributes when creating an object. The downside is that as of the publication of this book, you can create only the following object types: computer, contact, group, ou, quota, and user.

Using VBScript

The first step to create an object is to call GetObject on the parent container. Then call the Create method on that object and specify the objectClass and RDN for the new object. The sAMAccountName attribute is then set by using the Put method. Lastly, SetInfo commits the change. If SetInfo is not called, the creation will not be committed to the domain controller.

See Also

Recipe 15.15, MSDN: IADsContainer::GetObject, MSDN: IADsContainer::Create, MSDN: IADs::Put, and MSDN: IADs::SetInfo



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

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