Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
Set oRoot = GetObject("LDAP://RootDSE")
if getLastLogin(objNetwork.UserName) = "1-1-1970" then
msgbox "First login"
else
msgbox "loged in before"
end if
Function getLastLogin(Username)
lastDate = "1-1-1970"
sConfigNamingContext = oRoot.Get("configurationNamingContext")
Set oADOCommand = CreateObject("ADODB.Command")
Set oADOConnection = CreateObject("ADODB.Connection")
oADOConnection.Provider = "ADsDSOObject"
oADOConnection.Open "Active Directory Provider"
oADOCommand.ActiveConnection = oADOConnection
sADOQuery = "<LDAP://" & sConfigNamingContext & ">;(ObjectClass=nTDSDSA);AdsPath;subtree"
oADOCommand.CommandText = sADOQuery
oADOCommand.Properties("Page Size") = 100
oADOCommand.Properties("Timeout") = 30
oADOCommand.Properties("Cache Results") = False
Set oRecordSet = oADOCommand.Execute
Do Until oRecordSet.EOF
Set oDC = GetObject(GetObject(oRecordSet.Fields("AdsPath")).Parent)
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADSDSOObject"
conn.Open "ADs Provider"
strLDAP = "<LDAP://" & oDC.cn & ">;(&(objectCategory=User)(sAMAccountName=" & Username & "));adspath;subtree"
Set oComm = CreateObject("ADODB.Command")
oComm.ActiveConnection = conn
oComm.CommandText = strLDAP
Set rs = oComm.Execute
While Not rs.EOF
Set FoundObject = GetObject (rs.Fields(0).Value)
On error Resume Next
if datediff("s", lastDate, FoundObject.LastLogin) > 0 then
lastDate = FoundObject.LastLogin
end if
rs.MoveNext
Wend
oRecordSet.MoveNext
Loop
oADOConnection.Close
getLastLogin = lastDate
End Function 'getLastLogin
|