使用VBS更改Active Directory中的密码到期日期

时间:2011-04-05 18:41:19

标签: vbscript active-directory passwords

我正在尝试使用VBScript更改Active Directory中用户的密码到期日期。我有代码来获取有关用户密码的信息,但我找不到任何有关如何更改它的信息。任何帮助将不胜感激!

这是我的代码:

Const SEC_IN_DAY = 86400
Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000

Set objOU = GetObject("LDAP://CN=[username],OU=Users,OU=New York,OU=NA,OU=[domain],DC=[domain],DC=firm")

intCurrentValue = objOU.Get("userAccountControl")

If intCurrentValue and ADS_UF_DONT_EXPIRE_PASSWD Then
  wscript.echo "The password does not expire."
Else
  dtmValue = objOU.PasswordLastChanged 
  Wscript.echo "The password was last changed on " & _
  DateValue(dtmValue) & " at " & TimeValue(dtmValue) & VbCrLf & _
  "The difference between when the password was last set" & VbCrLf & _
  "and today is " & int(now - dtmValue) & " days"
  intTimeInterval = int(now - dtmValue)

  Set objDomainNT = GetObject("WinNT://ropesgray")
  intMaxPwdAge = objDomainNT.Get("MaxPasswordAge")
  If intMaxPwdAge < 0 Then
    WScript.Echo "The Maximum Password Age is set to 0 in the " & _
      "domain. Therefore, the password does not expire."
  Else
    intMaxPwdAge = (intMaxPwdAge/SEC_IN_DAY)
    Wscript.echo "The maximum password age is " & intMaxPwdAge & " days"
    If intTimeInterval >= intMaxPwdAge Then
      Wscript.echo "The password has expired."
    Else
      Wscript.echo "The password will expire on " & _
      DateValue(dtmValue + intMaxPwdAge) & " (" & _
      int((dtmValue + intMaxPwdAge) - now) & " days from today" & ")."
    End If
  End If
End If


'strUserPrincipalName = objOU.Get("userPrincipalName")
'strSAMAccountName = objOU.Get("sAMAccountName")
'strMaxPWAge = objOU.Get("manager")

'WScript.Echo strUserPrincipalName
'WScript.Echo strSAMAccountName
'WScript.Echo strMaxPWAge

1 个答案:

答案 0 :(得分:3)

您可以使用pwdLastSet属性更改密码过期,但可能不是您想要的方式。 pwdLastSet是自1601年1月1日凌晨12:00起的100纳秒间隔的数量。 根据{{​​3}},此属性仅接受两个值0或-1。

试试这个:

  1. pwdLastSet设置为0,这表示从未设置过密码。
  2. 然后,将pwdLastSet设置为-1,这表示刚刚设置了密码。因此,pwdLastSet中显示的值是当前日期/时间。
  3. 我习惯在W2K3中使用它,它仍在使用W2H8 R2。

    您可以Microsoft documentation(法语抱歉)允许您从1601年1月1日凌晨12:00开始,以100纳秒的间隔数创建日期/时间。

    小心它会延长密码持续时间,这对安全性不利。

    我希望它有所帮助。

    JP