Previous Section Table of Contents Next Section

The All-Purpose ADSI Object Creation Script

Creating objects in ADSI doesn't require a lot of code, either. Here's a generic, all-purpose script for creating a user.


strContainer = ""

strName = "UserName"



'generic part

Set objRootDSE = GetObject("LDAP://rootDSE")

If strContainer = "" Then

  Set objContainer = GetObject("LDAP://" & _

    objRootDSE.Get("defaultNamingContext"))

Else

  Set objContainer = GetObject("LDAP://" & _

    strContainer & "," & _

    objRootDSE.Get("defaultNamingContext"))

End If



'create the object

Set objUser = objContainer.Create("user", "cn=" & strName)

objUser.Put "sAMAccountName", strName

objUser.SetInfo

This script is pretty much exactly what the Microsoft EZADScriptomatic tool generates for you. Just fill in the container name with an organizational unit (OU) name or some other container name; otherwise, the user is created in the default location for your domain. Do you need to create a computer instead? Just change the last three lines of code to read like this.


Set objComputer = objContainer.Create("computer", "cn=" & strName)

objComputer.SetInfo

Do you want a new contact object? Here are the last couple of lines of code.


Set objContact = objContainer.Create("contact", "cn=" & strName)

objContact.SetInfo

New OUs are easy, too.


Set objOrganizationalunit = _

 objContainer.Create("organizationalUnit", "ou=" & strName)

objOrganizationalunit.SetInfo

Groups are only a bit tougher. Because there are multiple types of groups, you need to add this code to the beginning of your script.


ADS_GROUP_TYPE_GLOBAL_GROUP = &h2

ADS_GROUP_TYPE_LOCAL_GROUP = &h4

ADS_GROUP_TYPE_UNIVERSAL_GROUP = &h8

ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000

These definitions are Active Directory's standard numeric codes for group types. The last lines of code in the script would then be


Set objGroup = objContainer.Create("group", "cn=" & strName)

objGroup.Put "sAMAccountName", strName

objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _

  ADS_GROUP_TYPE_SECURITY_ENABLED

objGroup.SetInfo

Replace the boldfaced bit with the appropriate group type from the list you added to the beginning of your script.

    Previous Section Table of Contents Next Section