Ignore an error on vbs and continue

Hi
I have the attached a code that resets the administrator's password.
I want to know how i can modify it to ignore and continue forward with the rest of the script in case the script can't find the user "Administrator"? Since right now Im getting an error and the script stops (obviously since the user is missing).

Also, a way to indicate if the reset was successful in the form of exporting the username (Administrator) to a txt file?
strComputer = "." 
Set objUser = GetObject("WinNT://" & strComputer & "/Administrator")
objUser.SetPassword "PASSWORD"

Open in new window

johnnyjonathanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David LeeCommented:
Hi, johnnyjonathan.

Add the command

    On Error Resume Next

to the top of the script.
0
Shift-3Commented:
For more information, see this article on vbscript error handling.

You could use the script below to log errors to a file.


On Error Resume Next
 
strComputer = "." 
Set objUser = GetObject("WinNT://" & strComputer & "/Administrator")
objUser.SetPassword "PASSWORD"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.CreateTextFile("log.txt", True)
 
If Err.Number <> 0 Then
    objLog.WriteLine "Password reset unsuccessful."
    objLog.WriteLine "Error: " & Err.Number
    objLog.WriteLine "Error (Hex): " & Hex(Err.Number)
    objLog.WriteLine "Source: " &  Err.Source
    objLog.WriteLine "Description: " &  Err.Description
 
    Err.Clear
Else
    objLog.WriteLine "Password reset successful."
End If
 
objLog.Close

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Patrick MatthewsCommented:
Hello johnnyjonathan,

As noted by the Experts above, using the statement

      On Error Resume Next

tells VBScript to ignore runtime errors and continue with the next statement.  You can test for whether an
Error occurred by testing the Number property of the Err object; if it is 0, no error occurred.

This is the extent of VBScript's error handling, unlike VBA/VB6, in which you can branch out to error handling
routines.  To "turn off" this error handling, use:

      On Error GoTo 0

Regards,

Patrick
0
johnnyjonathanAuthor Commented:
Exactly what i was looking for ! thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.