Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2199
  • Last Modified:

Uncheck "password never expires" on a user account.

I made a tool for our support team that allows them to reset a user's AD password and require them to change it on next logon. The problem is that if "password never expires" is checked it will change the password but not require the user to change it at next logon. Is there a way to tell if it is checked and uncheck it if it is? Below is part of the code
DirectoryEntry de = GetUser(theUserToChange);
            try
            {
 
                if (de != null)
                {
                    string NewPassword = "password";
               // here I would like to check to see if "password never expires" is checked and if 
              // so uncheck it before setting the new password.
                    de.Invoke("SetPassword", new object[] { NewPassword });
                    de.Properties["pwdLastSet"].Value = 0;
                    de.CommitChanges();
                    Label3.Text = "User Password Reset.";
                    Label3.ForeColor = System.Drawing.Color.Green;
 
                }
                else
                {
                    Label3.Text = "User Not Found.";
                    Label3.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception exep)
            {
                string theError = exep.Message.ToString();
                Label3.Text = theError;
                Label3.ForeColor = System.Drawing.Color.Red;
            }

Open in new window

0
agcsupport
Asked:
agcsupport
1 Solution
 
Chris DentPowerShell DeveloperCommented:

Hey,

You need to pull the "userAccountControl" attribute, then look in it for a specific flag and potentially remove that flag.

So, the Password Never Expires flag can be found by performing a Bitwise And against the userAccountControl value. Something like this...

If (de.Properties["userAccountControl"].Value & 65536) {
  // This password never expires
  de.Properties["userAccountControl"].Value = de.Properties["userAccountControl"].Value ^ 65536;
  de.CommitChanges();
}

Values for the full set of flags associated with userAccountControl can be found here:

http://support.microsoft.com/kb/305144

You should see that Password Never Expires is listed as decimal 65536.

Chris
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now