Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET从active directory中删除用户_Vb.net_.net 2.0_Active Directory - Fatal编程技术网

VB.NET从active directory中删除用户

VB.NET从active directory中删除用户,vb.net,.net-2.0,active-directory,Vb.net,.net 2.0,Active Directory,您好,我正在尝试创建一个VB.NET应用程序,它(希望)将减少我在部门帮助热线电话上花费的时间。我遇到的问题是如何使用VB.NET从组中删除用户。以下是我一直在使用的代码: Public Shared Sub RemoveUserFromGroup(ByVal deUser As String, ByVal GroupName As String) Dim entry As DirectoryEntry = ADEntry() Dim mySearcher As Director

您好,我正在尝试创建一个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错误。使用group.invoke时,我收到错误targetInvoke异常未处理

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

我是VB.NET新手,希望大家能提供帮助

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

错误消息没有多大帮助。在使用Active Directory时,我经常感到沮丧

尝试更改行:

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

如果您需要更多的参考资料,请阅读Erika Ehrli的这篇文章。本文介绍了使用Active Directory的各种用例


我希望这会有所帮助。

给出错误消息和COM异常的HResult。Try:[code]:捕获ex作为System.DirectoryServices.directoryservicescomeexception:msgbox(例如Message()&例如HResult()&例如ErrorCode()):添加Try时结束Try。。。如您所建议,COM异常仍在发生,并且没有出现任何消息框。我已经将catch行更改为catch ex As System.Exception,这只是显示一个消息框,文本为“Unspecified Error-2147467259”。我会调查这个错误,看看能找到什么。谢谢,使用x.path.tostring()非常有效。经过许多天和无数次的代码变化,终于完成了代码的这一小部分,这真是太棒了。非常感谢您的帮助,非常感谢。不客气。我理解Active Directory和COM带来的挫折感。
group.Invoke("Remove", New Object() {x.Path.ToString()})