Partager via


Exemple de code pour l’ajout d’un membre à un groupe

Cette rubrique contient des exemples de code qui ajoutent un membre à un groupe. Les exemples Visual Basic Scripting Edition (VBScript) et C++ ajoutent un membre en ajoutant l’objet IADs qui représente le membre à l’objet IADsGroup qui représente le groupe. Les exemples Visual Basic .NET et C# modifient la propriété membre de l’objet DirectoryEntry qui représente le groupe.

Les exemples de code C# suivants ajoutent un membre existant à un groupe. La fonction prend L’ADsPath du conteneur de groupe et le nom unique du membre à ajouter au groupe. ADsPath est utilisé pour créer un objet DirectoryEntry qui représente le groupe. La méthode PropertyValueCollection.Add ajoute au groupe le membre dont le nom unique a été passé à la fonction. La fonction utilise ensuite la méthode DirectoryEntry.CommitChanges pour écrire les nouvelles informations de membre dans la base de données.

Appelez la fonction avec les paramètres suivants :

  • bindString: ADsPath valide pour un conteneur de groupe, tel que « LDAP://fabrikam.com/CN=TestGroup,OU=TestOU,DC=fabrikam,DC=com »
  • newMember: nom unique du membre à ajouter au groupe, tel que « CN=JeffSmith,OU=TestOU,DC=fabrikam,DC=com »
private void AddMemberToGroup(

                                string bindString,

                                string newMember

                                )

{

    try
    {

        DirectoryEntry ent = new DirectoryEntry( bindString );

        ent.Properties["member"].Add(newMember);

        ent.CommitChanges();

    }

    catch (Exception e)
    {

        Console.WriteLine( "An error occurred.");

        Console.WriteLine( "{0}", e.Message);

        return;

    }

}

Les exemples de code Visual Basic .NET suivants ajoutent un membre existant à un groupe. La fonction prend L’ADsPath du conteneur de groupe et le nom unique du membre à ajouter au groupe. ADsPath est utilisé pour créer un objet DirectoryEntry qui représente le groupe. La méthode PropertyValueCollection.Add ajoute au groupe le membre dont le nom unique a été passé à la fonction. La fonction utilise ensuite la méthode DirectoryEntry.CommitChanges pour écrire les nouvelles informations de membre dans la base de données.

Appelez la fonction avec les paramètres suivants :

  • bindString: ADsPath valide pour un conteneur de groupe, tel que « LDAP://fabrikam.com/CN=TestGroup,OU=TestOU,DC=fabrikam,DC=com »
  • newMember: nom unique du membre à ajouter au groupe, tel que « CN=JeffSmith,OU=TestOU,DC=fabrikam,DC=com »
Private Sub AddMemberToGroup(ByVal bindString As String, 
                                      ByVal newMember As String)

    Try

        Dim ent As New DirectoryEntry(bindString)

        ent.Properties("member").Add(newMember)

        ent.CommitChanges()

    Catch e As Exception

        Console.WriteLine("An error occurred.")

        Console.WriteLine("{0}", e.Message)

        Return

    End Try

End Sub

L’exemple VBScript suivant ajoute un membre existant à un groupe. Le script ajoute l’utilisateur, Jeff Smith, au groupe TestGroup.

Option Explicit

On Error Resume Next

Dim scriptResult        ' Script success or failure

Dim groupPath           ' ADsPath to the group container

Dim group               ' Group object

Dim memberPath          ' ADsPath to the member

Dim member              ' Member object

Dim groupMemberList     ' Used to display group members

Dim errorText           ' Error handing text

scriptResult = False

groupPath = "LDAP://fabrikam.com/CN=TestGroup,OU=TestOU,DC=fabrikam,DC=com"

memberPath = "LDAP://CN=JeffSmith,OU=TestOU,DC=fabrikam,DC=com"

WScript.Echo("Retrieving group object")

Set group = GetObject(groupPath)

If Err.number <> vbEmpty then

    Call ErrorHandler("Could not create group object.")

End If

Call ShowMembers(groupPath)     'Optional function call

WScript.Echo("Retrieving new member object")

Set member = GetObject(memberPath)

If Err.number <> vbEmpty then

    Call ErrorHandler("Could not get new member object.")

End If

WScript.Echo("Adding member to group.")

group.Add(member.ADsPath)

If Err.number <> vbEmpty then

    Call ErrorHandler("Could not add member to group.")

End If

Call ShowMembers(groupPath)     ' Optional function call

scriptResult = True

Call FinalResult(scriptResult)

'****************************************************************
' This function displays the members of a group. The function
' takes the ADsPath of the group.
'****************************************************************

Sub ShowMembers(groupPath)

    Dim groupMember

    Dim groupMemberList

    Dim groupObject

    Set groupObject = GetObject(groupPath)

    Set groupMemberList = groupObject.Members

    Select Case groupMemberList.Count

        Case 1 

            WScript.Echo vbcrlf & "The group has one member."

        Case 0

            WScript.Echo vbcrlf & "The group has no members."

        Case Else

            WScript.Echo vbcrlf & "The group has " & groupMemberList.Count & " members."

    End Select

    If groupMemberList.Count > 0 then

        WScript.Echo vbcrlf & "Here is a member list."

        For Each groupMember in groupMemberList

            WScript.Echo groupMember.Name

        Next

        WScript.Echo vbcrlf

    End If

    Set groupObject = Nothing

    Set groupMemberList = Nothing

End Sub

'****************************************************************
' This function shows if the script succeeded or failed. The 
' function processed the scriptResult variable.
'****************************************************************

Sub FinalResult(scriptResult)

    WScript.Echo vbcrlf

    If scriptResult = False then

        WScript.Echo "Script failed."

    Else

        WScript.Echo("Script successfully completed.")

    End If

    WScript.Quit

End Sub

'****************************************************************
' This function handles errors that occur in the script.
'****************************************************************

Sub ErrorHandler( errorText )

    WScript.Echo(vbcrlf & errorText)

    WScript.Echo("Error number: " & Err.number)

    WScript.Echo("Error Description: " & Err.Description)

    Err.Clear 

    Call FinalResult(scriptResult)

End Sub

L’exemple de code C++ suivant ajoute un membre existant à un groupe.

/////////////////////////////////////////////////////////////
/*  AddMemberToGroup() - Adds the passed directory object as a member 
                         of passed group
 
    Parameters
 
        IADsGroup * pGroup   - Group to hold the new IDirectoryObject.
        IADs* pIADsNewMember - Object which will become a member 
                               of the group. Object can be a user, 
                               contact, or group.
*/
HRESULT AddMemberToGroup(IADsGroup * pGroup, IADs* pIADsNewMember)
{
    HRESULT hr = E_INVALIDARG;
    if ((!pGroup)||(!pIADsNewMember))
        return hr;
 
    // Use the IADs::get_ADsPath() member to get the ADsPath.
    // When the ADsPath string is returned, to add the new
    // member to the group, call the IADsGroup::Add() member,
    // passing in the ADsPath string.
    // Query the new member for its AdsPath.
    // This is a fully qualified LDAP path to the object to add.
    BSTR bsNewMemberPath;
    hr = pIADsNewMember->get_ADsPath(&bsNewMemberPath); 
    if (SUCCEEDED(hr))
    {
 
        // Use the IADsGroup interface to add the new member.
        // Pass the LDAP path to the 
        // new member to the IADsGroup::Add() member
        hr = pGroup->Add(bsNewMemberPath);
 
        // Free the string returned from IADs::get_ADsPath()
        SysFreeString(bsNewMemberPath);
    }
    return hr;
}

ajout de membres à des groupes dans un de domaine

DirectoryEntry

DirectoryEntry.CommitChanges

DES IAD

IADsGroup

PropertyValueCollection.Add