protected override void Execute(CodeActivityContext context) { PrincipalContext principalContext; if (IsLocalAccount.Get(context)) { principalContext = new PrincipalContext(ContextType.Machine); } else { principalContext = new PrincipalContext(ContextType.Domain, Domain.Get(context), DomainContainer.Get(context)); } UserPrincipal principal = UserPrincipal.FindByIdentity(principalContext, Username.Get(context)); if (principal == null) { principal = new UserPrincipal(principalContext, Username.Get(context), Password.Get(context), true); principal.DisplayName = Firstname.Get(context) + " " + Lastname.Get(context); principal.PasswordNeverExpires = true; principal.Save(); IList <string> groups = GroupMemberships.Get(context) ?? new List <string>(); foreach (var group in groups) { GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, group); if (!groupPrincipal.Members.Contains(principal)) { groupPrincipal.Members.Add(principal); } groupPrincipal.Save(); } } else if (UpdateExistingUser.Get(context)) { principal.SetPassword(Password.Get(context)); principal.DisplayName = Firstname.Get(context) + " " + Lastname.Get(context); principal.PasswordNeverExpires = true; principal.Save(); IList <string> groups = GroupMemberships.Get(context) ?? new List <string>(); foreach (var group in groups) { GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, group); if (!groupPrincipal.Members.Contains(principal)) { groupPrincipal.Members.Add(principal); } groupPrincipal.Save(); } GroupPrincipal allGroups = new GroupPrincipal(principalContext); allGroups.Name = "*"; PrincipalSearcher searcher = new PrincipalSearcher(allGroups); var allGroupList = searcher.FindAll(); foreach (GroupPrincipal group in allGroupList) { if (!groups.Contains(group.Name) && group.Members.Contains(principal)) { group.Members.Remove(principal); group.Save(); } } } }