尝试访问PasswordLastChanged

时间:2015-06-12 17:32:50

标签: vbscript active-directory

我正在编写一个VBScript,如果他们的密码在给定的天数内被更改,它将只检查AD中的每个用户。当我试图让它为单个用户工作时,我提出了以下工作代码:

Option Explicit

Dim objUser, strLDAPConnection, intPwdExpLimit

strLDAPConnection = "CN=Test User,OU=Test,OU=Employees,DC=domain,DC=com"

intPwdExpLimit = 90

Set objUser = GetObject("LDAP://" + strLDAPConnection)

WScript.Echo DaysSincePwdChange(objUser)

Function DaysSincePwdChange(objUserAccount)
    DaysSincePwdChange = dateDiff("d", objUserAccount.PasswordLastChanged, Now)
End Function

然后我尝试通过使用以下代码循环遍历测试OU中的所有用户来使其工作:

Option Explicit

Const strOffice = "Test"

Dim objEmployeesOU, objUser, intPwdExpLimit

intPwdExpLimit = 90

Set objEmployeesOU = GetObject("LDAP://OU=" & strOffice & _
                     ",OU=Employees,DC=domain,DC=com")

For Each objUser In objEmployeesOU
    If objUser.class = "user" Then
        If ((DaysSincePwdChange(objUser)) >= intPwdExpLimit) Then
            MsgBox(objUser & ": Password Expired.")
        Else
            MsgBox(objUser & ": Password Current.")
        End If
    End If 
Next

Function DaysSincePwdChange(objUserAccount)
    DaysSincePwdChange = dateDiff("d", objUserAccount.PasswordLastChanged, Now)
End Function

上面的代码产生了一个0x8000500D错误并在Google上搜索错误,说它无法在缓存中找到该属性(请参阅PasswordLastSet属性,请参阅error description link here)。

为什么第一个代码块工作正常但第二个代码访问该属性有问题?

1 个答案:

答案 0 :(得分:3)

错误代码0x8000500d表示E_ADS_PROPERTY_NOT_FOUND。用户的密码从未更改过,因此未设置该属性。你可以像这样处理这样的情况:

Function DaysSincePwdChange(objUserAccount)
    On Error Resume Next
    DaysSincePwdChange = dateDiff("d", objUserAccount.PasswordLastChanged, Now)
    If Err Then
      If Err.Number = &h8000500d Then
        DaysSincePwdChange = -1
      Else
        WScript.Echo "Unexpected Error (0x" & Hex(Err.Number) & "): " & _
          Err.Description
        WScript.Quit 1
      End If
    End If
End Function

并像这样修改支票:

passwordAge = DaysSincePwdChange(objUser)
If passwordAge >= intPwdExpLimit) Then
    MsgBox(objUser & ": Password Expired.")
ElseIf passwordAge = -1 Then
    MsgBox(objUser & ": Password never changed.")
Else
    MsgBox(objUser & ": Password Current.")
End If