Click here to Skip to main content
15,940,921 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi I'm trying to write a program that adds a group to my local administrators group using vb.net. I found many articles talking about how to add a user, but only few of them talking about how to add a group. Everytime I try to run it, an error shows up at
Dim myGrp As DirectoryEntry = rootEntry.Children.Find(GrpName, "group")
And I'm not sure why.

What I have tried:

Dim localComp As string = System.Environment.MachineName()
Try
    Dim localMachine As New DirectoryEntry("WinNT://" & localComp & ",computer")
    Dim AdminGrp As DirectoryEntry = localMachine.Children.Find("Administrators", "group")

    Dim rootEntry As New DirectoryEntry("LDAP://DOMAINNAME")
    Dim myGrp As DirectoryEntry = rootEntry.Children.Find(GrpName, "group") 'ERROR

    AdminGrp.Invoke("Add", New Object() {myGrp.Path.ToString()})
    AdminGrp.CommitChanges()
    localMachine.Close()


Catch ex As Exception
    MessageBox.Show(ex.ToString)
End Try
Posted
Updated 6-Nov-18 17:42pm
Comments
Richard Deeming 26-Jun-18 14:11pm    
You'll probably find it easier to use the System.DirectoryServices.AccountManagement classes instead:
System.DirectoryServices.AccountManagement Namespace | Microsoft Docs[^]
Everything in Active Directory via C#.NET 3.5 (Using System.DirectoryServices.AccountManagement) - CodeProject[^]

But if you want someone to help you fix the error, then you're going to need to tell us what the error is. Click the green "Improve question" link and add the full details of the exception to your question.

1 solution

If possible, use System.DirectoryServices.AccountManagement instead.

VB
Dim LocalMachineContext As DirectoryServices.AccountManagement.PrincipalContext
Dim LocalGroupPrincipal As DirectoryServices.AccountManagement.GroupPrincipal
Dim DomainContext As DirectoryServices.AccountManagement.PrincipalContext
Dim DomainGroupPrincipal As DirectoryServices.AccountManagement.GroupPrincipal

' Get local machine context
LocalMachineContext = New DirectoryServices.AccountManagement.PrincipalContext(DirectoryServices.AccountManagement.ContextType.Machine)
' Get principal for local group
LocalGroupPrincipal = DirectoryServices.AccountManagement.GroupPrincipal.FindByIdentity(LocalMachineContext, "Administrators")

' Get domain context
DomainContext = New DirectoryServices.AccountManagement.PrincipalContext(DirectoryServices.AccountManagement.ContextType.Domain, "Domain", "DC=Domain,DC=Com", "UserName", "Password")
' Get principal for domain group
DomainGroupPrincipal = DirectoryServices.AccountManagement.GroupPrincipal.FindByIdentity(DomainContext, "DnsAdmins")

' Add domain group principal to local group members collection
LocalGroupPrincipal.Members.Add(DomainGroupPrincipal)
' Commit changes to local group
LocalGroupPrincipal.Save()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900