Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 884
  • Last Modified:

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
0
hpops
Asked:
hpops
  • 3
  • 3
1 Solution
 
AzraSoundCommented:
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
 
hpopsAuthor Commented:
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
 
AzraSoundCommented:
Where is the error occurring?  What is Line 51 in your script?
0
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!

 
hpopsAuthor Commented:
"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
 
EDDYKTCommented:
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
 
AzraSoundCommented:
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
 
hpopsAuthor Commented:
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now