VBscript没有输出尝试查询AD用户最近30天

时间:2013-12-19 21:15:08

标签: excel-vba vbscript vb6 vba excel

我有一个有效的查询,但它抓住了AD中的所有用户,我试图将其缩小到过去的90天。问题是即使查询确实运行,我也不再有任何输出。我知道我的数学错误,并且没有90天起飞。有人可以提供这方面的帮助吗?

        Dim currentDate
    currentDate = DateDiff("s", CDate("1/1/1970"), Now()) * 1000#
    currentDate = currentDate - 7776000000# 'Subtracts 90 days
'Does the query
    objCommand.CommandText = _
    "<LDAP://" & strDN & ">;" & _
    "(&(objectclass=user)(objectcategory=person)(lastLogonTimestamp<=" & currentDate & "));" & _
    "adspath,distinguishedname,sAMAccountName,lastLogonTimestamp,DisplayName,WhenCreated,userAccountControl;subtree"
'Output the query info
    Set objRecordSet = objCommand.Execute
    rngOut.CurrentRegion.Offset(2).ClearContents
    While Not objRecordSet.EOF
        rngOut.value = objRecordSet.Fields("DisplayName").value
        Set rngOut = rngOut.Offset(0, 1)
        rngOut.value = objRecordSet.Fields("sAMAccountName").value
        Set rngOut = rngOut.Offset(0, 1)
        rngOut.value = objRecordSet.Fields("WhenCreated").value
        Set rngOut = rngOut.Offset(0, 1)
        On Error Resume Next
        Set objDate = objRecordSet.Fields("lastLogonTimestamp").value
        If (Err.Number <> 0) Then
            On Error GoTo 0
            dtmDate = ""
        Else
            On Error GoTo 0
            lngHigh = objDate.HighPart
            lngLow = objDate.LowPart
            If (lngLow < 0) Then
                lngHigh = lngHigh + 1
            End If
            If (lngHigh = 0) And (lngLow = 0) Then
                dtmDate = ""
            Else
                dtmDate = #1/1/1601# + (((lngHigh * (2 ^ 32)) _
                    + lngLow) / 600000000) / 1440
            End If
        End If
        rngOut.value = dtmDate
        Set rngOut = rngOut.Offset(0, 1)
        rngOut.value = objRecordSet.Fields("distinguishedName").value
        Set rngOut = rngOut.Offset(0, 1)
        Set Uservar = objRecordSet.Fields("userAccountControl")
        If Uservar And 2 Then
            rngOut.value = "Disabled"
            rngOut.Font.ColorIndex = 3
        Else
            rngOut.value = "Enabled"
            rngOut.Font.ColorIndex = 0
        End If
        Set rngOut = rngOut.Offset(1, -5)
        objRecordSet.MoveNext
    Wend

我有这个工作,但只有当我将(lastLogonTimestamp<=" & currentDate & "));更改为(lastLogon<=" & currentDate & "));时,我想要的不会显示正确的用户群。谁能告诉我为什么?

1 个答案:

答案 0 :(得分:1)

VBScript tag wiki VBScript中所述,不会在字符串中扩展变量,因此您需要更改它:

objCommand.CommandText = _
  "<LDAP://" & strDN & ">;" & _
  "(&(objectclass=user)(objectcategory=person)(lastLogonTimestamp>=currentDate));" & _
  "adspath,distinguishedname,sAMAccountName,lastLogon,DisplayName,WhenCreated,userAccountControl;subtree"

进入这个:

objCommand.CommandText = _
  "<LDAP://" & strDN & ">;" & _
  "(&(objectclass=user)(objectcategory=person)(lastLogonTimestamp>=" & currentDate & "));" & _
  "adspath,distinguishedname,sAMAccountName,lastLogon,DisplayName,WhenCreated,userAccountControl;subtree"

此外,我建议将currentDate计算为正常Date值,例如像这样:

maxAge = 30 'days
currentDate = Now - maxAge

或者像这样:

maxAge = 30 'days
currentDate = DateAdd("d", -maxAge, Now)

然后使用Richard L. Mueller的this code将其转换为integer8值:

Function DateToInt8(d)
  biasKey  = CreateObject("Wscript.Shell").RegRead("HKLM\System" & _
    "\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias")
  If (UCase(TypeName(biasKey)) = "LONG") Then
    bias = biasKey
  ElseIf (UCase(TypeName(biasKey)) = "VARIANT()") Then
    bias = 0
    For k = 0 To UBound(biasKey)
      bias = bias + (biasKey(k) * 256^k)
    Next
  End If

  DateToInt8 = CStr(DateDiff("s", #1/1/1601#, DateAdd("n", bias, d))) & "0000000"
End Function

...

objCommand.CommandText = "<LDAP://" & strDN & ">;" & _
  "(&(objectclass=user)(objectcategory=person)(lastLogonTimestamp>=" & _
  DateToInt8(currentDate) & "));adspath,distinguishedname,sAMAccountName," & _
  "lastLogon,DisplayName,WhenCreated,userAccountControl;subtree"