?
Solved

Add an error handler to a vbscript

Posted on 2005-03-31
7
Medium Priority
?
882 Views
Last Modified: 2011-04-14
I have some code here that I'd like to have an error handler for. This script works fine if the following registry key exists HKEY_LOCAL_MACHINE\SYSTEM\Setup\OEMDuplicatorString\
however if the registry key does not exist I get an error on Line 51 Char 1, The error is: Object is not a collection.

Is there anyway to throw an exception that does not cause the script to error but creates an entry in the log that says something like "Reg key does not exist"?

Dim objShell, objFileSystem
Dim regComputerName, ComputerName
Dim CurrentDate, CurrentTime
CurrentDate = Date
CurrentTime = Time

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set StdOut = WScript.StdOut
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SYSTEM\Setup"
strValueName = "OEMDuplicatorString"
oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,_
strValueName,arrValues

oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath, strValueName,arrValues

'create the FileSystemObject
Set fso = CreateObject("scripting.filesystemobject")
'Open the data file for appending
'fso.OpenTextFile "c:\reglog.txt", ForAppending, True, TristateFalse
Set logts = fso.OpenTextFile("c:\reglog.txt", 8, -1, 0)

regComputerName = "HKLM\SYSTEM\CurrentControlSet\Control\" & _
"ComputerName\ComputerName\ComputerName"
Set objShell = CreateObject("WScript.Shell")
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
ComputerName = objShell.RegRead(regComputerName)

For Each strValue In arrValues
    'StdOut.WriteLine strValue
    'append to file
    logts.WriteLine ComputerName & " " & strValue & " " & "Recorded on " & CurrentDate & " " & "at " & CurrentTime
Next

'close the file
logts.Close
0
Comment
Question by:hpops
[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
  • 3
  • 3
7 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 13672599
VBScript does not have very good error handling.  All you can do is add On Error Resume Next at the top of your script, and periodically check for errors after certain function calls you feel may cause an error, e.g.,


On Error Resume Next

...


Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SYSTEM\Setup"
strValueName = "OEMDuplicatorString"
oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,_
strValueName,arrValues

If Err.Number <> 0 Then
    'error occurred - take action here
Else
    'continue with rest of processing here
End If
0
 

Author Comment

by:hpops
ID: 13674357
I have tried to make this work and am still getting a collection error when the file does not exist. I'm really lost on this one.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 13674715
Where is the error occurring?  What is Line 51 in your script?
0
Technology Partners: 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!

 

Author Comment

by:hpops
ID: 13674856
"For Each strValue In arrValues" is line 51. Sorry I stripped off the comments and forgot it would make a difference once I posted "Line 51" here. It's actually line 33 going by my first post.
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 1000 total points
ID: 13674909
You can use isarray to test


if (isarray(arrValues)) then
For Each strValue In arrValues
   'StdOut.WriteLine strValue
   'append to file
   logts.WriteLine ComputerName & " " & strValue & " " & "Recorded on " & CurrentDate & " " & "at " & CurrentTime
Next
else

logts.WriteLine ComputerName & " " & arrValues & " " & "Recorded on " & CurrentDate & " " & "at " & CurrentTime
end if
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 13675082
It should be glossing over that error, though, if you have added On Error Resume Next.  However, after the For...Next loop you should be able to test Err.Number and find that it does not equal 0.
0
 

Author Comment

by:hpops
ID: 13675451
Thanks everyone for helping, however my problem was resolved once I added the array to my existing code that EDDYKT mentioned above.
0

Featured Post

Technology Partners: 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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 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