获取 WinNT 组的成员列表

发布于 2024-07-07 10:46:40 字数 283 浏览 18 评论 0原文

关于堆栈溢出有几个与此类似的问题,但不完全相同。

我想在 win xp 计算机上打开或创建一个本地组并向其中添加成员、域、本地和众所周知的帐户。 我还想检查用户是否已经是会员,这样我就不会两次添加相同的帐户,并且可能会出现异常。

到目前为止,我开始将 DirectoryEntry 对象与 WinNT:// 提供程序一起使用。 一切顺利,但我不知道如何获取群组成员列表?

有人知道怎么做吗? 或者提供比使用 DirectoryEntry 更好的解决方案?

There are a couple of questions similar to this on stack overflow but not quite the same.

I want to open, or create, a local group on a win xp computer and add members to it, domain, local and well known accounts. I also want to check whether a user is already a member so that I don't add the same account twice, and presumably get an exception.

So far I started using the DirectoryEntry object with the WinNT:// provider. This is going ok but I'm stuck on how to get a list of members of a group?

Anyone know how to do this? Or provide a better solution than using DirectoryEntry?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薯片软お妹 2024-07-14 10:46:40

好吧,花了一段时间,尝试了不同的解决方案,但下面给出了最适合我原来问题的解决方案。 我无法使用“标准”方法让 DirectoryEntry 对象访问本地组的成员,我可以让它枚举成员的唯一方法是使用 Invoke 方法调用本机对象 Members 方法。

using(DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./Administrators,group"))
{
    foreach(object member in (IEnumerable) groupEntry.Invoke("Members"))
    {
        using(DirectoryEntry memberEntry = new DirectoryEntry(member))
        {
            Console.WriteLine(memberEntry.Path);
        }
    }
}

我还使用类似的技术在本地组中添加和删除成员。

希望这也对其他人有帮助。
基思.

Tim 编辑:添加了 VB.Net 版本

Public Function MembersOfGroup(ByVal GroupName As String) As List(Of DirectoryEntry)
    Dim members As New List(Of DirectoryEntry)
    Try
        Using search As New DirectoryEntry("WinNT://./" & GroupName & ",group")
            For Each member As Object In DirectCast(search.Invoke("Members"), IEnumerable)
                Dim memberEntry As New DirectoryEntry(member)
                members.Add(memberEntry)
            Next
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try
    Return members
End Function

Okay, it's taken a while, messing around with different solutions but the one that fits best with my original question is given below. I can't get the DirectoryEntry object to access the members of a local group using the 'standard' methods, the only way I could get it to enumerate the members was by using the Invoke method to call the native objects Members method.

using(DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./Administrators,group"))
{
    foreach(object member in (IEnumerable) groupEntry.Invoke("Members"))
    {
        using(DirectoryEntry memberEntry = new DirectoryEntry(member))
        {
            Console.WriteLine(memberEntry.Path);
        }
    }
}

I also used a similar technique to add and remove members from the local group.

Hopefully this helps someone else as well.
Keith.

EDIT by Tim: added VB.Net version

Public Function MembersOfGroup(ByVal GroupName As String) As List(Of DirectoryEntry)
    Dim members As New List(Of DirectoryEntry)
    Try
        Using search As New DirectoryEntry("WinNT://./" & GroupName & ",group")
            For Each member As Object In DirectCast(search.Invoke("Members"), IEnumerable)
                Dim memberEntry As New DirectoryEntry(member)
                members.Add(memberEntry)
            Next
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try
    Return members
End Function
云仙小弟 2024-07-14 10:46:40

Microsoft .NET Framework 提供了用于使用 Active Directory 的标准库:System.DirectoryServices 命名空间 在 System.DirectoryServices.dll 中。

Microsoft 建议使用 System.DirectoryServices 命名空间中的两个主要类:DirectoryEntryDirectorySearcher。 在大多数情况下,仅使用 DirectorySearcher 类就足够了。

更新:我在我的机器上测试了它 - 它有效。 但也许我误解了
你的问题。

以下是来自一篇出色的CodeProject 文章的示例:

获取属于特定 AD 组的用户列表

using System.DirectoryServices;

ArrayList GetADGroupUsers(string groupName)
{    
   SearchResult result;
   DirectorySearcher search = new DirectorySearcher();
   search.Filter = String.Format("(cn={0})", groupName);
   search.PropertiesToLoad.Add("member");
   result = search.FindOne();

   ArrayList userNames = new ArrayList();
   if (result != null)
   {
       for (int counter = 0; counter < 
          result.Properties["member"].Count; counter++)
       {
           string user = (string)result.Properties["member"][counter];
               userNames.Add(user);
       }
   }
   return userNames;
}

Microsoft .NET Framework provides a standard library for working with Active Directory: System.DirectoryServices namespace in the System.DirectoryServices.dll.

Microsoft recommends using two main classes from the System.DirectoryServices namespace: DirectoryEntry and DirectorySearcher. In most cases, it is enough to use DirectorySearcher class only.

UPDATE: I tested it on my machine - it works. But maybe I've misunderstood
your question.

Here is an example from an excellent CodeProject article:

Get a list of users belonging to a particular AD group

using System.DirectoryServices;

ArrayList GetADGroupUsers(string groupName)
{    
   SearchResult result;
   DirectorySearcher search = new DirectorySearcher();
   search.Filter = String.Format("(cn={0})", groupName);
   search.PropertiesToLoad.Add("member");
   result = search.FindOne();

   ArrayList userNames = new ArrayList();
   if (result != null)
   {
       for (int counter = 0; counter < 
          result.Properties["member"].Count; counter++)
       {
           string user = (string)result.Properties["member"][counter];
               userNames.Add(user);
       }
   }
   return userNames;
}
诺曦 2024-07-14 10:46:40

You should be able to find this information inside the "member" attribute on the DirectoryEntry that represents the group.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文