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.
Locked
User avatar
tonyf
Posts: 12
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Mon Aug 30, 2010 11:59 pm

Trouble with error handling

Post 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

jvierra
Posts: 14335
Joined: Tue May 22, 2007 9:57 am
Contact:

Trouble with error handling

Post 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

Locked