Directory Services

Adding an ADAM User to an ADAM Group

[This documentation is preliminary and subject to change.]

To add an ADAM user object to an ADAM group object, bind to the group and the user, and use the Add method on the group.

You can remove the object you add with the following code example by Removing an ADAM User from an ADAM Group.

The following VBScript code example uses the GetObject function to bind to an organization object, uses the GetObject function to bind to an group object in a organizationalUnit object of the organization and a user object in the organization, and uses the Add method to add the user to the group.

' Add ADAM User to ADAM Group.

Option Explicit

Dim objADAM   ' Binding object.
Dim objGroup  ' Group object.
Dim objUser   ' User object.
Dim strGroup  ' Group.
Dim strPath   ' Binding path.
Dim strOU	 ' Organizational unit.
Dim strUser   ' User.

' Construct ADAMsPath binding string.
' Change "localhost" to appropriate server.
' Change "389" to port for appropriate instance.
' Change "O=Fabrikam,C=US" to appropriate object.
strPath = "LDAP://localhost:389/O=Fabrikam,C=US"

WScript.Echo "Bind to: " & strPath

' Specify User.
strUser = "TestUser"

' Specify Group.
strGroup = "TestGroup"

' Specify Organizational Unit.
strOU = "TestOU"

WScript.Echo "Add:	 " & strUser
WScript.Echo "		to"
WScript.Echo "		 " & strGroup

On Error Resume Next

' Bind to the object.
Set objADAM = GetObject(strPath)

' Output error if the bind operation fails.
If Err.Number <> vbEmpty Then
	WScript.Echo "Error:   Bind failed."
	WScript.Quit
End If

' Add User to Group.
Set objGroup = objADAM.GetObject("group", "CN=" & strGroup & _
								 ",OU=" & strOU)
Set objUser  = objADAM.GetObject("user",  "CN=" & strUser)
objGroup.Add objUser.AdsPath

' Output success or error.
If Err.Number <> vbEmpty Then
	WScript.Echo "Error: Add failed."
Else
	WScript.Echo "Success: User added to group."
End If