2013-12-19 91 views
0

我有一個工作查詢,但它抓住了AD中的所有用戶,我試圖將其縮小到最後90天。問題是即使查詢運行,我也不再有任何輸出。我知道我的數學是錯誤的,並沒有脫落90天。任何人都可以提供協助嗎?VBscript過去30天沒有輸出嘗試查詢AD用戶

 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

我建議你使用[則DateDiff ](http://msdn.microsoft.com/en-us/library/xhtyw595(v = vs.84).aspx)進行日期計算。 – PatricK

+1

@PatricK Active Directory以不同格式存儲日期,因此用於日期計算的內置工具在此處的使用受到限制。 –

回答

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) 

,然後將其轉換爲從理查德L.米勒使用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" 
+0

嘿,謝謝你的代碼。我更新了我的代碼,但是當我運行它時,沒有輸出。我相信這是因爲數學是錯誤的,它試圖在未來找到用戶,而不是30天前。 –

+0

@aDroidman查看更新後的答案。 –

+0

我有它的工作,但它只適用於當我lastLogonTimestamp <=「&currentDate&」))lastLogon <=「&currentDate&」)) 這對我所做的沒有幫助。我已將代碼更新爲我正在使用的最新版本。 –

相關問題