IU5
asked on
Remove Domain Users Group from User in AD via VB.Net
Group,
Can someone help achieve this through VB.NET, I don't speak C#
I am able to add a user in AD, add a second security group, set the new group as default, but then I cannot remove the old default group. If I go thru AD interface the group will remove. I have tried several versions of code that all generate errors.
Any help appreciated.
Code -------------------------- ---------
Public Shared Sub RemoveDU(ByVal Login As String)
Dim de As DirectoryEntry = GetDirectoryEntry()
Dim deSearch As DirectorySearcher = New DirectorySearcher(de)
deSearch.Filter = "(&(objectClass=user)(sama ccountname =" & Login & "))"
deSearch.SearchScope = SearchScope.Subtree
Dim deSearchResult As SearchResult = deSearch.FindOne
If Not deSearchResult Is Nothing Then
Dim dey As DirectoryEntry = GetDirectoryEntry(deSearch Result.Pat h)
Dim deDomainUsers As DirectoryEntry = New DirectoryEntry("LDAP://xxx .xxx.xxx.x xx/CN=Doma in Users,CN=Users,DC=xxx,DC=o rg")
'Remove the User from "Domain Users" group
'deDomainUsers.Children.Re move(dey) - tried this as well
deDomainUsers.Properties(" member").R emove(dey. Path)
deDomainUsers.CommitChange s() - fails here
deDomainUsers.Close()
End If
deSearch.Dispose()
de.Close()
End Sub
Can someone help achieve this through VB.NET, I don't speak C#
I am able to add a user in AD, add a second security group, set the new group as default, but then I cannot remove the old default group. If I go thru AD interface the group will remove. I have tried several versions of code that all generate errors.
Any help appreciated.
Code --------------------------
Public Shared Sub RemoveDU(ByVal Login As String)
Dim de As DirectoryEntry = GetDirectoryEntry()
Dim deSearch As DirectorySearcher = New DirectorySearcher(de)
deSearch.Filter = "(&(objectClass=user)(sama
deSearch.SearchScope = SearchScope.Subtree
Dim deSearchResult As SearchResult = deSearch.FindOne
If Not deSearchResult Is Nothing Then
Dim dey As DirectoryEntry = GetDirectoryEntry(deSearch
Dim deDomainUsers As DirectoryEntry = New DirectoryEntry("LDAP://xxx
'Remove the User from "Domain Users" group
'deDomainUsers.Children.Re
deDomainUsers.Properties("
deDomainUsers.CommitChange
deDomainUsers.Close()
End If
deSearch.Dispose()
de.Close()
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER