?
Solved

Ignore an error on vbs and continue

Posted on 2009-04-13
4
Medium Priority
?
2,476 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 24129595
Hi, johnnyjonathan.

Add the command

    On Error Resume Next

to the top of the script.
0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 2000 total points
ID: 24129653
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 93

Expert Comment

by:Patrick Matthews
ID: 24129908
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
ID: 31569487
Exactly what i was looking for ! thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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