Page 1 of 1

Trouble with error handling

Posted: Mon Feb 04, 2013 6:25 am
by tonyf
Hi

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.

CODE:
'-----------------------------------------------
On Error Resume Next

'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
set ws = wscript.createObject("wscript.shell")
'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
oTextStream.Close

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"
Err.Clear

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

Next
'-------------------------------------------------

Thanks

Trouble with error handling

Posted: Mon Feb 04, 2013 6:44 am
by jvierra
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.

http://msdn.microsoft.com/en-us/library ... s.84).aspx