Solved

Ignore an error on vbs and continue

Posted on 2009-04-13
4
1,669 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:johnnyjonathan
4 Comments
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Hi, johnnyjonathan.

Add the command

    On Error Resume Next

to the top of the script.
0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 total points
Comment Utility
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
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
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
 

Author Closing Comment

by:johnnyjonathan
Comment Utility
Exactly what i was looking for ! thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

17 Experts available now in Live!

Get 1:1 Help Now