Esempio n. 1
0
        public static System.DirectoryServices.AccountManagement.GroupPrincipal New_DomainGroup(Args_New_DomainGroup args = null)
        {
            if (args == null)
            {
                args = new Args_New_DomainGroup();
            }

            var ContextArguments = new Args_Get_PrincipalContext
            {
                Identity   = args.SamAccountName,
                Domain     = args.Domain,
                Credential = args.Credential
            };
            var Context = GetPrincipalContext.Get_PrincipalContext(ContextArguments);

            if (Context != null)
            {
                var Group = new System.DirectoryServices.AccountManagement.GroupPrincipal(Context.Context);

                // set all the appropriate group parameters
                Group.SamAccountName = Context.Identity;

                if (!string.IsNullOrEmpty(args.Name))
                {
                    Group.Name = args.Name;
                }
                else
                {
                    Group.Name = Context.Identity;
                }
                if (!string.IsNullOrEmpty(args.DisplayName))
                {
                    Group.DisplayName = args.DisplayName;
                }
                else
                {
                    Group.DisplayName = Context.Identity;
                }

                if (!string.IsNullOrEmpty(args.Description))
                {
                    Group.Description = args.Description;
                }

                Logger.Write_Verbose($@"[New-DomainGroup] Attempting to create group '{args.SamAccountName}'");
                try
                {
                    Group.Save();
                    Logger.Write_Verbose($@"[New-DomainGroup] Group '{args.SamAccountName}' successfully created");
                    return(Group);
                }
                catch (Exception e)
                {
                    Logger.Write_Warning($@"[New-DomainGroup] Error creating group '{args.SamAccountName}' : {e}");
                }
            }

            return(null);
        }
Esempio n. 2
0
        public static void Add_DomainGroupMember(Args_Add_DomainGroupMember args = null)
        {
            if (args == null)
            {
                args = new Args_Add_DomainGroupMember();
            }

            var ContextArguments = new Args_Get_PrincipalContext
            {
                Identity   = args.Identity,
                Domain     = args.Domain,
                Credential = args.Credential
            };
            var GroupContext = GetPrincipalContext.Get_PrincipalContext(ContextArguments);

            System.DirectoryServices.AccountManagement.GroupPrincipal Group = null;
            if (GroupContext != null)
            {
                try
                {
                    Group = System.DirectoryServices.AccountManagement.GroupPrincipal.FindByIdentity(GroupContext.Context, GroupContext.Identity);
                }
                catch (Exception e)
                {
                    Logger.Write_Warning($@"[Add-DomainGroupMember] Error finding the group identity '{args.Identity}' : {e}");
                }
            }

            if (Group != null)
            {
                PrincipalContextEx UserContext = null;
                var UserIdentity = string.Empty;
                foreach (var Member in args.Members)
                {
                    if (Member.IsRegexMatch(@".+\\.+"))
                    {
                        ContextArguments.Identity = Member;
                        UserContext = GetPrincipalContext.Get_PrincipalContext(ContextArguments);
                        if (UserContext != null)
                        {
                            UserIdentity = UserContext.Identity;
                        }
                    }
                    else
                    {
                        UserContext  = GroupContext;
                        UserIdentity = Member;
                    }
                    Logger.Write_Verbose($@"[Add-DomainGroupMember] Adding member '{Member}' to group '{args.Identity}'");
                    Group.Members.Add(System.DirectoryServices.AccountManagement.Principal.FindByIdentity(UserContext.Context, UserIdentity));
                    Group.Save();
                }
            }
        }
Esempio n. 3
0
        public static void Set_DomainUserPassword(Args_Set_DomainUserPassword args = null)
        {
            if (args == null)
            {
                args = new Args_Set_DomainUserPassword();
            }

            var ContextArguments = new Args_Get_PrincipalContext
            {
                Identity   = args.Identity,
                Domain     = args.Domain,
                Credential = args.Credential
            };
            var Context = GetPrincipalContext.Get_PrincipalContext(ContextArguments);

            System.DirectoryServices.AccountManagement.UserPrincipal User = null;
            if (Context != null)
            {
                User = System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(Context.Context, args.Identity);

                if (User != null)
                {
                    Logger.Write_Verbose($@"[Set-DomainUserPassword] Attempting to set the password for user '{args.Identity}'");
                    try
                    {
                        var TempCred = new System.Net.NetworkCredential("a", args.AccountPassword);
                        User.SetPassword(TempCred.Password);
                        User.Save();
                        Logger.Write_Verbose($@"[Set-DomainUserPassword] Password for user '{args.Identity}' successfully reset");
                    }
                    catch (Exception e)
                    {
                        Logger.Write_Warning($@"[Set-DomainUserPassword] Error setting password for user '{args.Identity}' : {e}");
                    }
                }
                else
                {
                    Logger.Write_Warning($@"[Set-DomainUserPassword] Unable to find user '{args.Identity}'");
                }
            }
        }
Esempio n. 4
0
        public static System.DirectoryServices.AccountManagement.UserPrincipal New_DomainUser(Args_New_DomainUser args = null)
        {
            if (args == null)
            {
                args = new Args_New_DomainUser();
            }

            var ContextArguments = new Args_Get_PrincipalContext
            {
                Identity   = args.SamAccountName,
                Domain     = args.Domain,
                Credential = args.Credential
            };
            var Context = GetPrincipalContext.Get_PrincipalContext(ContextArguments);

            if (Context != null)
            {
                var User = new System.DirectoryServices.AccountManagement.UserPrincipal(Context.Context);

                // set all the appropriate user parameters
                User.SamAccountName = Context.Identity;
                var TempCred = new System.Net.NetworkCredential("a", args.AccountPassword);
                User.SetPassword(TempCred.Password);
                User.Enabled             = true;
                User.PasswordNotRequired = false;

                if (!string.IsNullOrEmpty(args.Name))
                {
                    User.Name = args.Name;
                }
                else
                {
                    User.Name = Context.Identity;
                }
                if (!string.IsNullOrEmpty(args.DisplayName))
                {
                    User.DisplayName = args.DisplayName;
                }
                else
                {
                    User.DisplayName = Context.Identity;
                }

                if (!string.IsNullOrEmpty(args.Description))
                {
                    User.Description = args.Description;
                }

                Logger.Write_Verbose($@"[New-DomainUser] Attempting to create user '{args.SamAccountName}'");
                try
                {
                    User.Save();
                    Logger.Write_Verbose($@"[New-DomainUser] User '{args.SamAccountName}' successfully created");
                    return(User);
                }
                catch (Exception e)
                {
                    Logger.Write_Warning($@"[New-DomainUser] Error creating user '{args.SamAccountName}' : {e}");
                }
            }

            return(null);
        }