Results 1 to 9 of 9
  1. #1
    New Lounger
    Join Date
    Jun 2001
    Location
    Orlando, FL
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error checking on Active Directory getobject (VB/VBScript)

    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.number<>0 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

    '************************************************* ***************

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Error checking on Active Directory getobject (VB/VBScript)

    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"?

  3. #3
    New Lounger
    Join Date
    Jun 2001
    Location
    Orlando, FL
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Error checking on Active Directory getobject (VB/VBScript)

    Gave the " If objUser Is Nothing then ..." a test, but no dice -- same as before.

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

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Error checking on Active Directory getobject (VB/VBScript)

    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.

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Error checking on Active Directory getobject (VB/VBScript)

    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?

  6. #6
    New Lounger
    Join Date
    Jun 2001
    Location
    Orlando, FL
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Error checking on Active Directory getobject (VB/VBScript)

    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

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Error checking on Active Directory getobject (VB/VBScript)

    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.

  8. #8
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Error checking on Active Directory getobject (VB/VBScript)

    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.
    Regards,
    Rory

    Microsoft MVP - Excel

  9. #9
    New Lounger
    Join Date
    Jun 2001
    Location
    Orlando, FL
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Error checking on Active Directory getobject (VB/VBScript)

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •