Link to home
Start Free TrialLog in
Avatar of hpops
hpops

asked on

Add an error handler to a vbscript

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
Avatar of AzraSound
AzraSound
Flag of United States of America image

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
Avatar of hpops
hpops

ASKER

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.
Where is the error occurring?  What is Line 51 in your script?
Avatar of hpops

ASKER

"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.
ASKER CERTIFIED SOLUTION
Avatar of EDDYKT
EDDYKT
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of hpops

ASKER

Thanks everyone for helping, however my problem was resolved once I added the array to my existing code that EDDYKT mentioned above.