Platform SDK: Active Directory, ADSI, and Directory Services

Creating Groups

To create a group, call the Create method of the object that will be the group's parent. For example:

Set newGroup = myComputer.Create("group", "GroupName")

The Create method takes two arguments: the type of object to create, and the name for the new object. After calling Create, the SetInfo method of the newly created group's parent object must be called to commit the change:

myComputer.SetInfo

The following example is a simple command line utility for creating groups. It takes three arguments: the ADsPath of an object to which you wish to add a group, the name of the group to create, and the new group's description. Note the use of On Error Resume Next to trap expected errors in the input arguments. For more detail about ADSI error handling, see Errors and Error Trapping.

Dim sADsPath
Dim sGroupName
Dim sDescription

Dim oTarget
Dim oNewGroup

On Error Resume Next

' Make sure the right number of arguments have been passed. If not, print
' the proper syntax for the script.
If WScript.Arguments.Count <> 3 Then
    WScript.Echo "Wrong number of arguments."
    WScript.Echo "Syntax:  newgroup.vbs <target> <name> <description>"
    WScript.Echo
    WScript.Echo "target       The ADsPath of the object to add the group to."
    WScript.Echo "name         Name for the new group."
    WScript.Echo "description  Description of the new Group."
    WScript.Quit(1)
End If

sADsPath = WScript.Arguments(0)
sGroupName = WScript.Arguments(1)
sDescription = WScript.Arguments(2)

' Bind to Computer object
Set oTarget = GetObject(sADsPath)
If Err Then AdsiErr()

' Create new User object.  SetInfo must be called here to commit the new
' group to the directory.
Set oNewGroup = oTarget.Create("group", sGroupName)
oNewGroup.SetInfo
If Err Then AdsiErr()

' Set basic properties on new group.  Note that SetInfo must be called a
' second time here to commit the Description property change.
oNewGroup.Description = sDescription
oNewGroup.SetInfo
If Err Then AdsiErr()

' Alert the user that the group has been created and return the Name and
' Description of the newly created group.  Note that GetInfo is called here
' to make sure we have the actual values of Name and Description and not any
' values that were put in the local property cache earlier.
oNewGroup.GetInfo
sGroupName = oNewGroup.Name
sDescription = oNewGroup.Description

WScript.Echo "New group " & sGroupName & " created."
WScript.Echo "Description: " & sDescription

' The AdsiErr subroutine handles any errors that might occur while creating
' the new group.  It handles the case where a group of the specified name
' already exists and the case where the specified group name is invalid.
' Any other error is reported as an unexpected error, then the script exits.
Sub AdsiErr()
    Dim s
    Dim e
    
    If Err.Number = &H80070563 Then
        s = "The group " & sGroupName & " already exists."
    ElseIf Err.Number = &H800A0408 Then
        s = "The name '" & sGroupName & "' is invalid as a group Name."
    Else
        e = Hex(Err.Number)
        s = "Unexpected Error " & e & "(" & Err.Number & ")"
    End If
    WScript.Echo s
    WScript.Quit(1)

End Sub