【问题标题】:seeking example using ADSI API to programmatically create a Windows Group寻找使用 ADSI API 以编程方式创建 Windows 组的示例
【发布时间】:2011-11-10 19:29:21
【问题描述】:

寻找使用 ADSI API 以编程方式创建 Windows 组的示例。 AD 是 Windows Active Directory http://en.wikipedia.org/wiki/Active_Directory

“SI”可能是服务接口?

无论如何,这个领域只是没有很好的记录。 . .我看过一些 PowerShell 脚本。 . .但真的不想确保已安装 PowerShell 等。运行并确保将 MY_XYZ_GROUP 添加到 Window 的组集的简单程序.....

应该很容易......看起来并不那么容易。

【问题讨论】:

  • 根据您的编程语言,您可能需要考虑使用 NetLocalGroupAdd 而不是 ADSI。

标签: windows adsi


【解决方案1】:

ADSI = Active Directory Service Interfaces - 这是一个与 Active Directory 对话以在 Active Directory(Microsoft 网络的基于网络的 LDAP 目录)中创建用户、组和计算机帐户的 API。

那么您需要在本地机器/服务器上创建本地用户,还是需要在 Active Directory 中创建组??

如果您使用 .NET 3.5 及更高版本进行编程,您应该查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:

基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

if(user != null)
{
   // do something here....     
}

// create a group 
GroupPrincipal group = new GroupPrincipal(ctx, "Group01");
// set other properties on the group here.....
group.Save();  

新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易!

更新:很遗憾,新的 S.DS.AM 不适用于本地组 :-( 它仅适用于 Active Directory。

如果您需要创建本地 Windows 组,则需要使用旧的 DirectoryEntry 方法 - 类似于:

// bind to your machine's WinNT:// provider
DirectoryEntry computer = new DirectoryEntry("WinNT://YourMachineNameHere");

// create a new local group on your computer
DirectoryEntry newGroup = computer.Children.Add("NewGroupName", "Group");

// save that group to the local machine
newGroup.CommitChanges();

// refresh the property cache so you can set properties like "Description" or others
newGroup.RefreshCache();
newGroup.Properties["description"].Value = "Description for your group....";
newGroup.CommitChanges();

Richard Mueller 有一个great list of Excel sheets 显示所有可用的属性,包括基于 LDAP 的 Active Directory 对象以及非常有限的 WinNT 属性。

【讨论】:

  • Marc_s -- 这看起来很有希望,今晚晚些时候会深入挖掘。我的目标是添加 1 个本地 Windows 组(如果还没有的话)。所以是的 - 我需要在本地机器上创建本地组(和下一步本地用户)。如果已建立域网络。此 GROUP 创建需要能够工作(即域服务器不应撤消我的创建。)我将不得不研究您的示例 - 非常感谢!
【解决方案2】:

NetLocalGroupAddMembers (Windows API) 是我一直在寻找的 API,与 ADSI 方法相比,它的工作量要少得多。按照 MSDN 中的记录进行编码和测试并工作。

不清楚为什么我在所有相关和广泛的互联网搜索中都没有这个 API - 一个原因是我没有包括“本地”。

这是 MSDN 链接: http://msdn.microsoft.com/en-us/library/windows/desktop/aa370436(v=VS.85).aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 2014-09-26
    • 1970-01-01
    • 2011-04-07
    • 2014-03-19
    • 1970-01-01
    相关资源
    最近更新 更多