Creazione di gruppi

In questo argomento viene illustrato come creare diversi tipi di gruppi.

Quando si crea un nuovo gruppo, è possibile utilizzare flag dell'enumerazione ADS_GROUP_TYPE_ENUM per assegnare un tipo al gruppo, ad esempio globale (ADS_GROUP_TYPE_GLOBAL_GROUP), dominio locale (ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP), locale (ADS_GROUP_TYPE_LOCAL_GROUP), universale (ADS_GROUP_TYPE_UNIVERSAL_GROUP) o abilitato per la sicurezza (ADS_GROUP_TYPE_SECURITY_ENABLED). Se non si specifica un tipo di gruppo, per impostazione predefinita viene creato un gruppo globale e protetto (ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_GLOBAL_GROUP | ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_SECURITY_ENABLED). Per ulteriori informazioni sull'enumerazione ADS_GROUP_TYPE_ENUM, vedere l'argomento relativo in MSDN Library all'indirizzo https://go.microsoft.com/fwlink/?LinkID=27252 (informazioni in lingua inglese).

Nell'esempio di codice Visual Basic .NET riportato di seguito viene illustrato come creare un nuovo gruppo, chiamato Practice Managers, in un'unità organizzativa chiamata Consulting. Normalmente in un dominio l'attributo sAMAccountName è obbligatorio ma in un dominio Windows Server 2003 o successivo l'attributo sAMAccountName è facoltativo. Per ulteriori informazioni sull'attributo sAMAccountName, vedere gli argomenti relativi in MSDN Library all'indirizzo https://go.microsoft.com/fwlink/?LinkID=27252 (informazioni in lingua inglese).

' 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()

Nell'esempio di codice C# riportato di seguito viene illustrato come creare un nuovo gruppo, chiamato Practice Managers, nell'unità organizzativa chiamata Consulting. Normalmente in un dominio l'attributo sAMAccountName è obbligatorio ma in un dominio Windows Server 2003 o successivo l'attributo sAMAccountName è facoltativo. Per ulteriori informazioni sull'attributo sAMAccountName, vedere le pagine relative all'attributo sAMAccountName o SAM-Account-Name in MSDN Library all'indirizzo https://go.microsoft.com/fwlink/?LinkID=27252.

// 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();

Nell'esempio di codice Visual Basic .NET riportato di seguito viene illustrato come creare un gruppo di dominio locale, chiamato Managers, nell'unità organizzativa chiamata Consulting. Utilizzare Utilizzo di COM Interop per accedere a ADSI per specificare il flag 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()

Nell'esempio di codice C# riportato di seguito viene illustrato come creare un gruppo di dominio locale, chiamato Managers, nell'unità organizzativa chiamata Consulting. Utilizzare Utilizzo di COM Interop per accedere a ADSI per specificare il flag 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();

Nell'esempio di codice Visual Basic .NET riportato di seguito viene illustrato come creare un gruppo non correlato alla sicurezza, ovvero un elenco di distribuzione chiamato Full Time Employees, nell'unità organizzativa Consulting. Utilizzare Utilizzo di COM Interop per accedere a ADSI per specificare il flag 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()

Nell'esempio di codice C# riportato di seguito viene illustrato come creare un gruppo non di sicurezza, che è un elenco di distribuzione chiamato Full Time Employees, nell'unità organizzativa Consulting. Utilizzare Utilizzo di COM Interop per accedere a ADSI per specificare il flag 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();

Nell'esempio di codice Visual Basic .NET riportato di seguito viene illustrato come aggiungere un gruppo intero a un altro gruppo.

' 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()

Nell'esempio di codice C# riportato di seguito viene illustrato come aggiungere un gruppo intero a un altro gruppo.

// 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();

Vedere anche

Riferimenti

System.DirectoryServices

Concetti

Gestione di gruppi
Utilizzo di COM Interop per accedere a ADSI

Send comments about this topic to Microsoft.

Copyright © 2007 Microsoft Corporation. Tutti i diritti riservati.