Skip to content

Instantly share code, notes, and snippets.

@kmhuglen
Created September 14, 2016 11:53
Show Gist options
  • Save kmhuglen/717d7f1bac11b54d94d77140ba81f354 to your computer and use it in GitHub Desktop.
Save kmhuglen/717d7f1bac11b54d94d77140ba81f354 to your computer and use it in GitHub Desktop.
Option Explicit
Dim objRootDSE, adoConnection, adoCommand, strQuery
Dim adoRecordset, strDNSDomain, objShell, lngBiasKey
Dim lngBias, k, strDN, dtmDate, objDate
Dim strBase, strFilter, strAttributes, lngHigh, lngLow
Dim objFileSystem, objOutputFile, strOutputFile
strOutputFile = "./" & Split(WScript.ScriptName, ".")(0) & ".csv"
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile(strOutputFile, TRUE)
` Obtain local Time Zone bias from machine registry.
` This bias changes with Daylight Savings Time.
Set objShell = CreateObject("Wscript.Shell")
lngBiasKey = objShell.RegRead("HKLMSystemCurrentControlSetControl" _
& "TimeZoneInformationActiveTimeBias")
If (UCase(TypeName(lngBiasKey)) = "LONG") Then
lngBias = lngBiasKey
ElseIf (UCase(TypeName(lngBiasKey)) = "VARIANT()") Then
lngBias = 0
For k = 0 To UBound(lngBiasKey)
lngBias = lngBias + (lngBiasKey(k) * 256^k)
Next
End If
Set objShell = Nothing
` Determine DNS domain from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
Set objRootDSE = Nothing
` Use ADO to search Active Directory.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
` Search entire domain.
strBase = "<LDAP://" & strDNSDomain & ">"
` Filter on all user objects.
strFilter = "(&(objectCategory=person)(objectClass=user))"
` Comma delimited list of attribute values to retrieve.
strAttributes = "distinguishedName,lastLogonTimeStamp"
` Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
` Run the query.
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 60
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute
objOutputFile.WriteLine("DN" & ";" & "Date")
` Enumerate resulting recordset.
Do Until adoRecordset.EOF
` Retrieve attribute values for the user.
strDN = adoRecordset.Fields("distinguishedName").Value
` Convert Integer8 value to date/time in current time zone.
On Error Resume Next
Set objDate = adoRecordset.Fields("lastLogonTimeStamp").Value
If (Err.Number <> 0) Then
On Error GoTo 0
dtmDate = #1/1/1601#
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 = #1/1/1601#
Else
dtmDate = #1/1/1601# + (((lngHigh * (2 ^ 32)) _
+ lngLow)/600000000 – lngBias)/1440
End If
End If
` Display values for the user.
If (dtmDate = #1/1/1601#) Then
objOutputFile.WriteLine(strDN & ";Never")
Else
objOutputFile.WriteLine(strDN & ";" & dtmDate)
End If
adoRecordset.MoveNext
Loop
` Clean up.
objOutputFile.Close
Set objFileSystem = Nothing
adoRecordset.Close
adoConnection.Close
WScript.Quit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment