Directory Services

Creating Groups

To create a group, call the Create method of the prospective group parent object, as shown in the following 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 object must be called to commit the change.

myComputer.SetInfo

The following code example is a command line utility for creating groups. It takes three arguments: the ADsPath (binding string, such as WinNT://mymachine) of an object to which you wish to add a group, the name of the group to create, and the new group description. Be aware that the use of On Error Resume Next to trap expected errors in the input arguments. For more information about ADSI error handling, see Errors and Error Trapping.

Dim sADsPath
Dim sGroupName
Dim sDescription

Dim oTarget
Dim oNewGroup

On Error Resume Next

' Be sure that the right number of arguments were 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 the Computer object.
Set oTarget = GetObject(sADsPath)
If Err Then AdsiErr()

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

' Set basic properties for new group. Call SetInfo again 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 new group. Be aware that GetInfo is called to ensure that
' the actual values of Name and Description exist and not any values 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 errors that 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 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