VBScript LDAP Question

Posted on 2008-11-03
Last Modified: 2013-12-24
I am writing a small vbscript tool and have a question...

When I query using ldp my active directory server there is a propery on the user called pwdLastSet... I can see it just fine.
When I run this code I get that it isnt a valid property..

'Get Domain Information
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")
strContainer = "CN=Users ,"
'Check that filename was passed as argument
If Wscript.Arguments.Count = 0 Then
            Wscript.Echo "Usage: cscript ADExport.vbs <filename>"
End If
strFilename = WScript.Arguments(0)
'Open Text File for writing
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile(strFilename, True)
strContainer = strContainer & strDNSDomain
set objOU =GetObject("LDAP://" & strContainer )
For each objUser in objOU
            If objUser.class="user" then
                        If objUser.userAccountControl = 514 Then
                                    ObjTextFile.WriteLine objUser.sAMAccountName & ",1," & objUser.Info & "," & objUser.mail & "," & objuser.pwdLastSet & "," & objUser.userAccountControl
                                    ObjTextFile.WriteLine objUser.sAMAccountName & ",0," & objUser.Info & "," & objUser.mail & "," & objuser.pwdLastSet & "," & objUser.userAccountControl
                        End If
            End if

So the question here is WHY cant I get to this property?

Question by:NTGuru705
    1 Comment
    LVL 18

    Accepted Solution

    That's because the value is stored as Integer8 (64-bit), not as a date.  You have to convert it.  The error you were getting was "Type Mismatch", right?

    The code below should work for you.

    Set oDate = objUser.pwdLastSet
    LastSet = Integer8Date(oDate)
    Function Integer8Date(ByVal objDate)
        lngHigh = objDate.HighPart
        lngLow = objdate.LowPart
        If (lngLow < 0) Then lngHigh = lngHigh + 1
        If (lngHigh = 0) And (lngLow = 0) Then lngAdjust = 0
        lngDate = #1/1/1601# + (((lngHigh * (2 ^ 32)) + lngLow) / 600000000 - lngAdjust) / 1440
        Integer8Date = CDate(lngDate)
    End Function

    Open in new window


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
    This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now