共用方式為


建立群組

本主題示範如何建立多種類型的群組。

當您建立新的群組時,可以使用 ADS_GROUP_TYPE_ENUM 列舉中的旗標,將群組類型指派給群組,例如全域 (ADS_GROUP_TYPE_GLOBAL_GROUP)、網域本機 (ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP)、本機 (ADS_GROUP_TYPE_LOCAL_GROUP)、萬用 (ADS_GROUP_TYPE_UNIVERSAL_GROUP) 或啟用安全性 (ADS_GROUP_TYPE_SECURITY_ENABLED)。如果未指定群組類型,則預設會建立全域安全群組 (ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_GLOBAL_GROUP | ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_SECURITY_ENABLED)。如需 ADS_GROUP_TYPE_ENUM 列舉的詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<ADS_GROUP_TYPE_ENUM>。

下列 Visual Basic .NET 程式碼範例示範如何在組織單位 Consulting 中建立新的群組 Practice Managers。在網域中,sAMAccountName 屬性是必要屬性,但是在 Windows Server 2003 或更新版本的網域中,sAMAccountName 屬性則是選用屬性。如需 sAMAccountName 屬性的詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<sAMAccountName>或<SAM-Account-Name 屬性>。

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

下列 C# 程式碼範例示範如何在組織單位 Consulting 中建立新的群組 Practice Managers。在網域中,sAMAccountName 屬性是必要屬性,但是在 Windows Server 2003 或更新版本的網域中,sAMAccountName 屬性則是選用屬性。如需 sAMAccountName 屬性的詳細資訊,請參閱 MSDN Library 中的<sAMAccountName>或<SAM-Account-Name 屬性>,網址為: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();

下列 Visual Basic .NET 程式碼範例示範如何在組織單位 Consulting 中建立本機網域群組 Managers。請使用 使用 COM Interop 存取 ADSI 指定 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()

下列 C# 程式碼範例示範如何在組織單位 Consulting 中建立本機網域群組 Managers。請使用 使用 COM Interop 存取 ADSI 指定 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();

下列 Visual Basic .NET 程式碼範例示範如何在組織單位 Consulting 中建立非安全性群組 (這是名為 Full Time Employees 的通訊群組清單)。請使用 使用 COM Interop 存取 ADSI 指定 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()

下列 C# 程式碼範例示範如何在組織單位 Consulting 中建立非安全性群組 (這是名為 Full Time Employees 的通訊群組清單)。請使用 使用 COM Interop 存取 ADSI 指定 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();

下列 Visual Basic .NET 程式碼範例示範如何將整個群組加入至另一個群組。

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

下列 C# 程式碼範例示範如何將整個群組加入至另一個群組。

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

請參閱

參考

System.DirectoryServices

概念

群組管理
使用 COM Interop 存取 ADSI

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation.All rights reserved.