Search code examples
vb.netactive-directoryexelocaldirectoryentry

How to add a group to local administrators group using VB.NET?


I'm trying to write a program that adds a group named grp1 to the local administrators group using VB.NET. I have tried the ways mentioned in this post: Adding group to local administrators, but it didn't work for me. Below is what I have so far:

Function AddToLocalAdmin(grp1 As String)
    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")

        AdminGrp.Invoke("Add", grp1)
        AdminGrp.CommitChanges()

        Return True
    Catch ex As Exception
        Return False
    End Try
End Function

I found many articles on Google talking about how to add an User to local administrators group, but what should I do to add a Group to it?

Thanks in advance.


Solution

  • Nvm I found the solution myself. The code below worked perfectly fine.

        AdminGrp.Invoke("Add", New Object() {"WinNT://DOMAINNAME/" & grp1})