I am trying to add a network group on our domain to the local administrators group on the machine.
I debug line by line and it finds the local administrator group, it finds the network group but when it gets to this line:
AdministratorsGrp.Members.Add(NetworkGrp)
It returns an error "The network path was not found"
How do I fix this error, I spent 30 minutes searching the interwebs and couldn't find a solution :(
Here is my function thus far:
Private Function AddAdminGroup() As Boolean
Dim AdministratorsGrp As GroupPrincipal
Dim NetworkGrp As GroupPrincipal
Try
Dim DomainCtx As New PrincipalContext(ContextType.Domain, "<domainname>", "<username>", "<pw>")
'Find Local Administrators group by SID
AdministratorsGrp = GroupPrincipal.FindByIdentity(DomainCtx, IdentityType.Sid, "S-1-5-32-544")
'Find Desktop Managers group by Name on Domain
NetworkGrp = GroupPrincipal.FindByIdentity(DomainCtx, IdentityType.Name, "<network group")
'Add Desktop Managers group to Administrators Group
AdministratorsGrp.Members.Add(DesktopMGRGrp)
'Save Group
AdministratorsGrp.Save()
Return True
Catch PrinEx As PrincipalExistsException
Return True
Catch ex As Exception
End Try
Return False
End Function
Thanks in advance for any help provided!!
I fixed this problem by changing this line:
Dim DomainCtx As New PrincipalContext(ContextType.Domain, "<domainname>", "<username>", "<pw>")
to
Dim DomainCtx As New PrincipalContext(ContextType.Domain)
I'm not sure why but providing credentials for the domain was causing a problem.