I've been trying to find a way to add cached domain user to local admin group. Every solution seems to require connection to the domain. Is there a way to get user object from a local cache based on name or username and add them to local Administrators group? Please help.
This is the code that works with the domain controller available. Any advice would be much appreciated.
Dim lclctx As PrincipalContext
Dim domctx As PrincipalContext
Dim grp As GroupPrincipal
Try
lclctx = New PrincipalContext(ContextType.Machine, Environment.MachineName)
domctx = New PrincipalContext(ContextType.Domain, "DOMAIN")
grp = GroupPrincipal.FindByIdentity(lclctx, IdentityType.Name, "Administrators")
If Not grp Is Nothing Then
grp.Members.Add(domctx, IdentityType.Name, strName)
grp.Save()
grp.Dispose()
End If
lclctx.Dispose()
domctx.Dispose()
Catch ex As Exception
writeToLog(ex.Message.ToString)
End Try