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.
This topic is 11 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
tonyf
Posts: 12
Last visit: Fri May 03, 2013 9:57 am

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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
This topic is 11 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked