Partager via


Création de groupes

Cette rubrique explique comment créer différents types de groupes.

Lorsque vous créez un groupe, vous pouvez utiliser des indicateurs de l'énumération ADS_GROUP_TYPE_ENUM pour affecter un type de groupe au groupe, tel que global (ADS_GROUP_TYPE_GLOBAL_GROUP), domaine local (ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP), local (ADS_GROUP_TYPE_LOCAL_GROUP), universel (ADS_GROUP_TYPE_UNIVERSAL_GROUP) ou avec sécurité (ADS_GROUP_TYPE_SECURITY_ENABLED). Si vous ne spécifiez pas de type de groupe, le groupe sera créé par défaut en tant que groupe global et sécurisé (ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_GLOBAL_GROUP | ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_SECURITY_ENABLED). Pour plus d'informations sur l'énumération ADS_GROUP_TYPE_ENUM, voir « ADS_GROUP_TYPE_ENUM » dans MSDN Library à l'adresse https://go.microsoft.com/fwlink/?LinkID=27252.

L'exemple de code Visual Basic .NET suivant montre comment créer un groupe appelé Practice Managers dans une unité d'organisation appelée Consulting. Dans un domaine, l'attribut sAMAccountName est obligatoire, mais dans un domaine Windows Server 2003 ou ultérieur, l'attribut sAMAccountName est facultatif. Pour plus d'informations sur l'attribut sAMAccountName, voir « sAMAccountName » ou « SAM-Account-Name attribute » dans MSDN Library à l'adresse https://go.microsoft.com/fwlink/?LinkID=27252.

' Bind to the domain that this user is currently connected to.
Dim dom As New DirectoryEntry()

' Find the container (in this case, the Consulting organizational unit) that you 
' wish to add the new group to.
Dim ou As DirectoryEntry = dom.Children.Find("OU=Consulting")

' Add the new group Practice Managers.
Dim group As DirectoryEntry = ou.Children.Add("CN=Practice Managers", "group")

' Set the samAccountName for the new group.
group.Properties("samAccountName").Value = "pracmans"

' Commit the new group to the directory.
group.CommitChanges()

L'exemple de code C# suivant montre comment créer un groupe appelé Practice Managers dans l'unité d'organisation appelée Consulting. Dans un domaine, l'attribut sAMAccountName est obligatoire, mais dans un domaine Windows Server 2003 ou ultérieur, l'attribut sAMAccountName est facultatif. Pour plus d'informations sur l'attribut sAMAccountName, consultez « sAMAccountName » ou « SAM-Account-Name attribute » dans MSDN Library à l'adresse https://go.microsoft.com/fwlink/?LinkID=27252 (page pouvant être en anglais).

// Bind to the domain that this user is currently connected to.
DirectoryEntry dom = new DirectoryEntry();

// Find the container (in this case, the Consulting organizational unit) that you 
// wish to add the new group to.
DirectoryEntry ou = dom.Children.Find("OU=Consulting");

// Add the new group Practice Managers.
DirectoryEntry group = ou.Children.Add("CN=Practice Managers", "group");

// Set the samAccountName for the new group.
group.Properties["samAccountName"].Value = "pracmans";

// Commit the new group to the directory.
group.CommitChanges();

L'exemple de code Visual Basic .NET suivant montre comment créer un groupe de domaine local appelé Managers dans l'unité d'organisation appelée Consulting. Utilisez Utilisation de COM Interop pour accéder à l'interface ADSI pour spécifier l'indicateur ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP.

' Bind to the domain that this user is currently connected to.
Dim dom As New DirectoryEntry()

' Find the container (in this case, the Consulting organizational unit) that you 
' wish to add the new local domain group to.
Dim ou As DirectoryEntry = dom.Children.Find("OU=Consulting")

' Add the Managers group.
Dim mgr As DirectoryEntry = ou.Children.Add("CN=Managers", "group")

' Set the group type to a secured domain local group.
mgr.Properties("groupType").Value = ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP Or ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_SECURITY_ENABLED

' Commit the new group to the directory.
mgr.CommitChanges()

L'exemple de code C# suivant montre comment créer un groupe de domaine local appelé Managers dans l'unité d'organisation appelée Consulting. Utilisez Utilisation de COM Interop pour accéder à l'interface ADSI pour spécifier l'indicateur ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP.

// Bind to the domain that this user is currently connected to.
DirectoryEntry dom = new DirectoryEntry();

// Find the container (in this case, the Consulting organizational unit) that you 
// wish to add the new local domain group to.
DirectoryEntry ou = dom.Children.Find("OU=Consulting");

// Add the Managers group.
DirectoryEntry mgr = ou.Children.Add("CN=Managers", "group");

// Set the group type to a secured domain local group.
mgr.Properties["groupType"].Value = ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP | 
ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_SECURITY_ENABLED;

// Commit the new group to the directory.
mgr.CommitChanges();

L'exemple de code Visual Basic .NET suivant montre comment créer un groupe hors sécurité, à savoir une liste de distribution appelée Full Time Employees, dans l'unité d'organisation appelée Consulting. Utilisez Utilisation de COM Interop pour accéder à l'interface ADSI pour spécifier l'indicateur ADS_GROUP_TYPE_GLOBAL_GROUP.

' Bind to the domain that this user is currently connected to.
Dim dom As New DirectoryEntry()

' Find the container (in this case, the Consulting organizational unit) that you
' wish to add the Full Time Employees distribution list to.
Dim ou As DirectoryEntry = dom.Children.Find("OU=Consulting")

' Add the Full Time Employees distribution list.
Dim dl As DirectoryEntry = ou.Children.Add("CN=Full Time Employees", "group")

' Set the group type to global.
dl.Properties("groupType").Value = ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_GLOBAL_GROUP

' Commit the new group to the directory.
dl.CommitChanges()

L'exemple de code C# suivant montre comment créer un groupe hors sécurité, à savoir une liste de distribution appelée Full Time Employees, dans l'unité d'organisation appelée Consulting. Utilisez Utilisation de COM Interop pour accéder à l'interface ADSI pour spécifier l'indicateur ADS_GROUP_TYPE_GLOBAL_GROUP.

// Bind to the domain that this user is currently connected to.
DirectoryEntry dom = new DirectoryEntry();

// Find the container (in this case, the Consulting organizational unit) that you
// wish to add the Full Time Employees distribution list to.
DirectoryEntry ou = dom.Children.Find("OU=Consulting");

// Add the Full Time Employees distribution list.
DirectoryEntry dl = ou.Children.Add("CN=Full Time Employees", "group");

// Set the group type to global.
dl.Properties["groupType"].Value = ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_GLOBAL_GROUP;

// Commit the new group to the directory.
dl.CommitChanges();

L'exemple de code Visual Basic .NET suivant montre comment ajouter un groupe entier à un autre groupe.

' Bind to the domain that this user is currently connected to.
Dim dom As New DirectoryEntry()

' Find the container (in this case, the North America group) that you
' wish to add.
Dim group As DirectoryEntry = dom.Children.Find("CN=North America")

' Connect to the group that you wish to add "group" to.
Dim mgr As New DirectoryEntry("LDAP://CN=Managers,OU=Consulting,DC=Fabrikam,DC=COM")

' Add the distinguishedName of "group" to the members property of "mgr".
mgr.Properties("member").Add(group.Properties("distinguishedName").Value)

' Commit the changes to the directory.
mgr.CommitChanges()

L'exemple de code C# suivant montre comment ajouter un groupe entier à un autre groupe.

// Bind to the domain that this user is currently connected to.
DirectoryEntry dom = new DirectoryEntry();

// Find the container (in this case, the North America group) that you
// wish to add.
DirectoryEntry group = dom.Children.Find("CN=North America");

// Connect to the group that you wish to add "group" to.
DirectoryEntry mgr = new DirectoryEntry("LDAP://CN=Managers,OU=Consulting,DC=Fabrikam,DC=COM");

// Add the distinguishedName of "group" to the members property of "mgr".
mgr.Properties["member"].Add(group.Properties["distinguishedName"].Value);

// Commit the changes to the directory.
mgr.CommitChanges();

Voir aussi

Référence

System.DirectoryServices

Concepts

Gestion de groupes
Utilisation de COM Interop pour accéder à l'interface ADSI

Send comments about this topic to Microsoft.

Copyright © 2007 par Microsoft Corporation. Tous droits réservés.