获取 WinNT 组的成员列表

Get a list of members of a WinNT group(获取 WinNT 组的成员列表)
本文介绍了获取 WinNT 组的成员列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

有几个问题与堆栈溢出类似,但不完全相同.

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

到目前为止,我开始将 DirectoryEntry 对象与 WinNT:// 提供程序一起使用.一切正常,但我一直在纠结如何获取群组成员列表?

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

解决方案

好的,花了一段时间,尝试了不同的解决方案,但下面给出了最适合我最初问题的解决方案.我无法使用标准"方法让 DirectoryEntry 对象访问本地组的成员,我可以让它枚举成员的唯一方法是使用 Invoke 方法调用本机对象的成员方法.<前>using(DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./Administrators,group")){foreach((IEnumerable) groupEntry.Invoke("Members") 中的对象成员){using(DirectoryEntry memberEntry = new DirectoryEntry(member)){Console.WriteLine(memberEntry.Path);}}}

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

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

EDIT by Tim:添加了 VB.Net 版本

公共函数MembersOfGroup(ByVal GroupName As String) As List(Of DirectoryEntry)Dim 成员作为新列表(Of DirectoryEntry)尝试使用搜索作为新的 DirectoryEntry("WinNT://./" & GroupName & ",group")For Each member As Object In DirectCast(search.Invoke("Members"), IEnumerable)Dim memberEntry 作为新的 DirectoryEntry(成员)members.Add(memberEntry)下一个结束使用Catch ex 作为例外MessageBox.Show(ex.ToString)结束尝试回归会员结束函数

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?

解决方案

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

这篇关于获取 WinNT 组的成员列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

ActiveDirectory error 0x8000500c when traversing properties(遍历属性时 ActiveDirectory 错误 0x8000500c)
search by samaccountname with wildcards(使用通配符按 samaccountname 搜索)
Get the list of Groups for the given UserPrincipal(获取给定 UserPrincipal 的组列表)
Can you find an Active Directory User#39;s Primary Group in C#?(你能在 C# 中找到 Active Directory 用户的主要组吗?)
How to register System.DirectoryServices for use in SQL CLR User Functions?(如何注册 System.DirectoryServices 以在 SQL CLR 用户函数中使用?)
Query From LDAP for User Groups(从 LDAP 查询用户组)