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\OEMD uplicatorS tring\
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:{ impersonat ionLevel=i mpersonate }!\\" &_
strComputer & "\root\default:StdRegProv" )
strKeyPath = "SYSTEM\Setup"
strValueName = "OEMDuplicatorString"
oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyP ath,_
strValueName,arrValues
oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyP ath, strValueName,arrValues
'create the FileSystemObject
Set fso = CreateObject("scripting.fi lesystemob ject")
'Open the data file for appending
'fso.OpenTextFile "c:\reglog.txt", ForAppending, True, TristateFalse
Set logts = fso.OpenTextFile("c:\reglo g.txt", 8, -1, 0)
regComputerName = "HKLM\SYSTEM\CurrentContro lSet\Contr ol\" & _
"ComputerName\ComputerName \ComputerN ame"
Set objShell = CreateObject("WScript.Shel l")
Set objFileSystem = CreateObject("Scripting.Fi leSystemOb ject")
ComputerName = objShell.RegRead(regComput erName)
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
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:{
strComputer & "\root\default:StdRegProv"
strKeyPath = "SYSTEM\Setup"
strValueName = "OEMDuplicatorString"
oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyP
strValueName,arrValues
oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyP
'create the FileSystemObject
Set fso = CreateObject("scripting.fi
'Open the data file for appending
'fso.OpenTextFile "c:\reglog.txt", ForAppending, True, TristateFalse
Set logts = fso.OpenTextFile("c:\reglo
regComputerName = "HKLM\SYSTEM\CurrentContro
"ComputerName\ComputerName
Set objShell = CreateObject("WScript.Shel
Set objFileSystem = CreateObject("Scripting.Fi
ComputerName = objShell.RegRead(regComput
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
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
Thanks everyone for helping, however my problem was resolved once I added the array to my existing code that EDDYKT mentioned above.
On Error Resume Next
...
Set oReg=GetObject("winmgmts:{
strComputer & "\root\default:StdRegProv"
strKeyPath = "SYSTEM\Setup"
strValueName = "OEMDuplicatorString"
oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyP
strValueName,arrValues
If Err.Number <> 0 Then
'error occurred - take action here
Else
'continue with rest of processing here
End If