Recipe 16.9. Setting a User's Account OptionsProblemYou want to view or update the userAccountControl attribute for a user. This attribute controls various account options; for example, the user must change his password at next logon and whether the account is disabled. SolutionUsing a graphical user interface
Using a command-line interfaceThe dsmod user command has several options for setting various userAccountControl flags, which are listed in the Discussion section. Each switch accepts yes or no as a parameter to either enable or disable the setting. Using VBScript' This code enables or disables a bit value in the userAccountControl attr. ' ------ SCRIPT CONFIGURATION ------ strUserDN = "<UserDN>" ' e.g., cn=rallen,ou=Sales,dc=rallencorp,dc=com intBit = <BitValue> ' e.g., 65536 boolEnable = <TrueOrFalse> ' e.g., trUE ' ------ END CONFIGURATION --------- strAttr = "userAccountControl" set objUser = GetObject("LDAP://" & strUserDN) intBitsOrig = objUser.Get(strAttr) intBitsCalc = CalcBit(intBitsOrig, intBit, boolEnable) if intBitsOrig <> intBitsCalc then objUser.Put strAttr, intBitsCalc objUser.SetInfo WScript.Echo "Changed " & strAttr & " from " & _ intBitsOrig & " to " & intBitsCalc else WScript.Echo "Did not need to change " & strAttr & " (" & _ intBitsOrig & ")" end if Function CalcBit(intValue, intBit, boolEnable) CalcBit = intValue if boolEnable = TRUE then CalcBit = intValue Or intBit else if intValue And intBit then CalcBit = intValue Xor intBit end if end if End FunctionDiscussionThe userAccountControl attribute on user (and computer) objects could be considered the kitchen sink of miscellaneous and sometimes completely unrelated user account properties. If you have to do much creating and managing user objects, you'll need to become intimately familiar with this attribute. The userAccountControl attribute is a bit flag, which means you have to take a couple extra steps to search against or modify it. For more on searching and modifying a bit flag attribute, see Recipes 4.10 and 4.13 in Active Directory Cookbook (O'Reilly). The dsmod user command can be used to modify a subset of userAccountControl properties, as shown in Table 16-1. Table 16-2 lists userAccountControl properties as defined in the ADS_USER_FLAG_ENUM enumeration.
See AlsoMSDN: ADS_USER_FLAG_ENUM |