VB.NET 从活动目录中删除用户

发布于 2024-09-12 19:42:15 字数 1768 浏览 3 评论 0原文

您好,我正在尝试创建一个 VB.NET 应用程序,它(希望)可以减少我的部门帮助台呼叫所花费的时间。我所困惑的部分是如何使用 VB.NET 从组中删除用户。以下是我一直在使用的代码:

Public Shared Sub RemoveUserFromGroup(ByVal deUser As String, ByVal GroupName As String)
    Dim entry As DirectoryEntry = ADEntry()
    Dim mySearcher As DirectorySearcher = New DirectorySearcher(entry)

    mySearcher.Filter = "(&(ObjectClass=Group)(CN=" & GroupName & "))"
    mySearcher.PropertiesToLoad.Add("OrganizationalUnit")
    mySearcher.PropertiesToLoad.Add("DistinguishedName")
    mySearcher.PropertiesToLoad.Add("sAMAccountName")

    Dim searchResults As SearchResultCollection = mySearcher.FindAll()
    If searchResults.Count > 0 Then
        Dim group As New DirectoryEntry(searchResults(0).Path)
        Dim members As Object = group.Invoke("Members", Nothing)
        For Each member As Object In CType(members, IEnumerable)
            Dim x As DirectoryEntry = New DirectoryEntry(member)
            MessageBox.Show(x.Properties("sAMAccountName").Value)
            If x.Properties("sAMAccountName").Value = deUser Then
                MessageBox.Show(searchResults.Item(0).Path.ToString)
                MessageBox.Show(x.Properties("sAMAccountName").Value)
                'group.Invoke("Remove", New Object() {x.Properties("OrganizationalUnit").Value})
                group.Properties("member").Remove(x.Properties("OrganizationalUnit").Value)
            End If

        Next
    End If

当我运行程序时,我在 group.properties 行收到 COMException was unhandled, unspecified 错误。使用 group.invoke 时,我收到错误 TargetInitationException 未处理。

我的目标是将用户名 (sAMAccountName) 和组名 (sAMAccountName) 作为字符串传递给函数,该函数将找到用户并将其从组中删除。

我是 VB.NET 的新手,非常感谢人们可以提供的任何帮助。

我正在使用 .NET 2.0 进行编码,因为我不确定它将运行的服务器是否安装了 3.5。

Hi I am trying to create a VB.NET application which will (hopefully) reduce some time spent on some of my departments helpdesk calls. The part that I am stuck with is how to use VB.NET to remove a user from a group. The following is code that I have been playing with:

Public Shared Sub RemoveUserFromGroup(ByVal deUser As String, ByVal GroupName As String)
    Dim entry As DirectoryEntry = ADEntry()
    Dim mySearcher As DirectorySearcher = New DirectorySearcher(entry)

    mySearcher.Filter = "(&(ObjectClass=Group)(CN=" & GroupName & "))"
    mySearcher.PropertiesToLoad.Add("OrganizationalUnit")
    mySearcher.PropertiesToLoad.Add("DistinguishedName")
    mySearcher.PropertiesToLoad.Add("sAMAccountName")

    Dim searchResults As SearchResultCollection = mySearcher.FindAll()
    If searchResults.Count > 0 Then
        Dim group As New DirectoryEntry(searchResults(0).Path)
        Dim members As Object = group.Invoke("Members", Nothing)
        For Each member As Object In CType(members, IEnumerable)
            Dim x As DirectoryEntry = New DirectoryEntry(member)
            MessageBox.Show(x.Properties("sAMAccountName").Value)
            If x.Properties("sAMAccountName").Value = deUser Then
                MessageBox.Show(searchResults.Item(0).Path.ToString)
                MessageBox.Show(x.Properties("sAMAccountName").Value)
                'group.Invoke("Remove", New Object() {x.Properties("OrganizationalUnit").Value})
                group.Properties("member").Remove(x.Properties("OrganizationalUnit").Value)
            End If

        Next
    End If

When I run the program, I recevie a COMException was unhandled, unspecified error at the group.properties line. When using group.invoke I receive the error TargetInvocationException was unhandled.

My aim is to pass as a string the username (sAMAccountName) and the groupname (sAMAccountName) to the function which will locate the user and remove them from the group.

I am new to VB.NET and would appreciate any assistance people can provide.

I am coding in .NET 2.0 as I am unsure if the server it will live on will have 3.5 installed.

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

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

发布评论

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

评论(1

开始看清了 2024-09-19 19:42:15

那么错误消息 0x80004005 E_FAIL Unspecified failure 并不是很有帮助。在使用 Active Directory 时,我经常感到沮丧。

尝试将行:更改

group.Properties("member").Remove(x.Properties("OrganizationalUnit").Value)

group.Invoke("Remove", New Object() {x.Path.ToString()})

如果您需要更多参考,请查看 VB 上的这篇文章.net Heaven 作者:Erika Ehrli。本文涵盖了 Active Directory 的各种用例。

我希望这有帮助。

Well the error message 0x80004005 E_FAIL Unspecified failure is not very helpful. I often get frustrated when working with Active Directory.

Try changing line:

group.Properties("member").Remove(x.Properties("OrganizationalUnit").Value)

to

group.Invoke("Remove", New Object() {x.Path.ToString()})

If you need more reference take a look at this article on VB.net Heaven by Erika Ehrli. The article covers various use cases with Active Directory.

I hope that helps.

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