VB.NET从Active Directory中删除用户用户、NET、VB、Directory

2023-09-08 13:29:45 作者:午夜搭车

您好我想创建一个VB.NET应用程序,将(希望)减少一些时间花在我的一些部门的求助电话。那我坚持的部分是如何使用VB.NET从组中删除用户。以下是code,我一直在玩的:

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

当我运行这个程序,我recevie一个收到COMException是在group.properties线未处理,未指定的错误。当使用group.invoke我收到错误TargetInvocationException是未处理的。

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.

我的目的是要通过一个字符串的用户名(sAMAccountName赋)和组名(sAMAccountName赋),以将定位用户,并从该组删除的功能。

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.

我是新来的VB.NET和将AP preciate任何帮助的人可以提供。

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

我编码在.NET 2.0,因为我如果服务器将生活将有3.5安装无法确定。

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

推荐答案

那么错误信息的 0x80004005的E_FAIL未指定的故障是不是非常有帮助。我与Active Directory工作时经常会感到沮丧。

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

尝试改变行:

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

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

如果你需要更多的参考看看这篇文章对 VB.net天堂通过埃里卡Ehrli。本文涵盖各种应用情况与Active Directory。

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.

我希望帮助。