• Error checking on Active Directory getobject (VB/VBScript)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Error checking on Active Directory getobject (VB/VBScript)

    Author
    Topic
    #374278

    I need help with this, as I can’t see what I’m doing wrong:

    I’m wrote a VBScript file that unlocks a user from Active Directory. I got everything to work, but I wanted to add some error control to the script. So, I added the “On Error Resume Next” at the beginning of my script and then put in test conditions. Here is the code snippet:

    set objUser = getobject(“WinNT://GTLAW/” & strusername & “,user”)
    ‘ Check to make sure username is valid
    if err.number 0 then
    wscript.echo(“Error accessing user: ” & strusername & ” — not found”)
    wscript.quit
    end if

    The problem is when I ran this without the “on error resume next” the script would fail at the getobject line with an error window saying ” line 43, char 5, The user name could not be found, code 800708AD, source (null) “. Once you add the “On error” line, no error is generated and the script just ends — no error message, no anything.

    Any ideas on how I should be properly testing in this instance?

    I’m attaching the full script in case you need it for reference. Please go easy, as I am still learning the ropes ….

    Thanks! Andrew

    Here is the full script:

    ‘****************************************************************
    ‘ ADUnlock.vbs

    ‘ Written by Andrew Harrell on 7/15/02

    ‘ The script clears the “Account is Locked Out” flag for a specific user
    ‘ in Active Directory (for GTLAW)

    ‘ The syntax for running this script is: cscript adunlock.vbs username
    ‘ where username is the person who’s account you are trying to unlock


    ‘****************************************************************

    option explicit

    call main()

    ‘****************************************************************
    ‘* End of script
    ‘****************************************************************
    ‘****************************************************************
    ‘* Subs and Functions
    ‘****************************************************************

    sub main
    dim strusername
    dim objUser

    ‘ Check for correct number of arguments in the command line

    if wscript.arguments.count 1 then
    wscript.echo “Syntax error: Usage is: cscript adunlock.vbs username”
    wscript.quit
    end if

    strusername = wscript.arguments(0)

    ‘Now bind to the Active Directory user object

    set objUser = getobject(“WinNT://GTLAW/” & strusername & “,user”)

    ‘ Check to make sure username is valid

    if err.number 0 then
    wscript.echo(“Error accessing user: ” & strusername & ” — not found”)
    wscript.quit
    end if

    ‘ Now test for a locked out status, and if locked go ahead and unlock

    if objUser.IsAccountLocked = True then
    wscript.echo(“User ” & strusername & ” is locked, now unlocking …”)
    objUser.IsAccountLocked = False
    objuser.SetInfo
    if err.number0 then
    wscript.echo (“Cannot Save Change to Active Directory — Possible Security Issue”)
    wscript.quit
    end if
    wscript.echo(“User ” & strusername & ” has been unlocked!”)
    else
    wscript.echo(“User ” & strusername & ” is not locked out in Active Directory.”)
    end if

    end sub

    ‘****************************************************************

    Viewing 0 reply threads
    Author
    Replies
    • #605115

      On Error Resume Next should work in VBScript in general. I’m pretty sure it works with cscript.exe as well, but don’t have the energy to test it out tonight.

      As a Plan B for the GetObject problem, does it work to test “If objUser Is Nothing”?

      • #605167

        Gave the ” If objUser Is Nothing then …” a test, but no dice — same as before.

        Anything else to try? Again, thanks for whatever help!!

        • #605367

          I don’t know anything about AD, but maybe the security context somehow forbids use of GetObject??? Try CreateObject and see if that does the trick…or creates other problems.

        • #605368

          After re-reading your original post, I think my above reply is probably not helpful. If you put a MsgBox err.number immediately after your GetObject call, does it run? If so, does it report anything unusual?

          • #605494

            nothing — the MsgBox does not come up at all.

            Could this be something to do with the fact I’m dealing with an object , and therefore I might have to do some kind of test with vbObjectError and do an Err.Raise? I’m starting to wonder if the getobject call is doing something on the server side (calling to Active Directory), and the server is passing back an error that kills the script on the getobject call. Does that make any sense?

            I’m just kinda groping here …. thanks

            • #605581

              Sorry I can’t help more. Kevin of the youthful user picture has done some AD scripting, I vaguely recall, but otherwise, maybe post a link to this thread on the Windows 2000 board for more help.

            • #605601

              Hi Andrew,
              I’m not sure how helpful this will be, but I’ve just run your script against my local account on my WinXP machine and it works fine if I put my username in and produces the expected error message if I put in a bogus user, so perhaps it is a remote server issue. The only other thing I can think of (which is probably irrelevant!) is that all ADSI scripts I’ve seen use the syntax:
              if (err.number 0) Then
              which may help in forcing evaluation of the statement?
              FWIW.

            • #605635

              first, thanks to all who took time to look at this for me …

              Rory, I’ll give your syntax a try, but I suspect that will not do anything. I’m thinking that the script dies on the getobject line. As a test, I put a msgbox right after the getobject line (no conditional code). It never displayed the msgbox, it just halted execution of the script. Arrgh!!! I wonder if there is a way to test before doing the getobject — some kind of object.exists ….

              I’ll keep working at it and if I come up with anything I will post it … thanks

    Viewing 0 reply threads
    Reply To: Error checking on Active Directory getobject (VB/VBScript)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: