Trouble with error handling

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
Posts: 12
Joined: Mon Aug 30, 2010 11:59 pm

Trouble with error handling

Post by tonyf » Mon Feb 04, 2013 7:25 am


I'm trying to install a registry key only if the registry key does not already exist but when I run the code the script installs the key regardless.

On Error Resume Next

'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
set ws = wscript.createObject("")
'open the data file
Set oTextStream = oFSO.OpenTextFile("wslist.txt")
'make an array from the data file
RemotePC = Split(oTextStream.ReadAll, vbNewLine)
'close the data file

For Each strComputer In RemotePC
strCMD = "CMD /k" & " " & "REG DELETE " & strComputer & "HKLMSoftwareTestKey"
ws.Run strCMD

If Err.Number 0 Then
'WScript.Echo strComputer & " " & "Error: " & Err.Number
WScript.Echo strComputer & " " & "Registry key found"

strCMD2 = "CMD /k" & " " & "REG ADD " & strComputer & "HKLMSoftwareTestKey"
ws.Run strCMD2
End If



User avatar
Posts: 13128
Joined: Tue May 22, 2007 9:57 am

Trouble with error handling

Post by jvierra » Mon Feb 04, 2013 7:44 am

That is because of a number of things. First you are always deleting the key. Second you are never testing the key.

With VBScript ther eis no need to use REG externally. This is much harder to manage. Use ReqRead/RegWrite to check and update the registry. ... s.84).aspx