troubleshooting Question

VBScript to check registry for existence of a value

Avatar of Darthyw
Darthyw asked on
Operating Systems
7 Comments1 Solution5445 ViewsLast Modified:
I'm trying to run the script below to check for a registry value and if it exists to run the registry imports and exports.  It runs regardless of whether the value exists in the registry or not.  This is probably only the second VBScript I've written, so I am quite new to this.  I'm using a function I found on the internet to check for the value existence, but I've got something awry.

Option Explicit
On Error Resume Next
dim objShell,objFSO, objFolder, regRDP, regTDP, RDP, TDP, strRegKey
Set objShell = CreateObject("WScript.Shell")

 Function RegEntryExists(theEntry)
   On error resume next
   set shell = CreateObject("WScript.Shell")
   x = shell.RegRead(theEntry)
   WScript.Echo Err.Number
   If Err.Number <> 0 then
      RegEntryExists = 1
      RegEntryExists = 0
   end if
End Function

If RegEntryExists = 1 Then
  WScript.Echo("It exists.")
  WScript.Echo("It doesn't exist.")
end if

If RegEntryExists("HKLM\System\CurrentControlSet\Enum\Root\LEGACY_RDPWD\0000", "Class") = 0 Then
      'Create folder C:\regbkup
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objFolder = objFSO.CreateFolder("C:\regbkup")
      'Make backups of the registry keys under C:\regbkup
      WScript.Echo ("Beginning Export " & Now)
      objShell.Exec "%comspec% /k reg.exe EXPORT HKEY_LOCAL_MACHINE\System\CurrentControlSet\Enum\Root\LEGACY_TDTCP C:\regbkup\tdpenum.reg"
      objShell.Exec "%comspec% /k reg.exe EXPORT HKEY_LOCAL_MACHINE\System\CurrentControlSet\Enum\Root\LEGACY_RDPWD C:\regbkup\rdpenum.reg"
      objShell.Exec "%comspec% /k reg.exe EXPORT HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TDTCP C:\regbkup\tdpserv.reg"
      objShell.Exec "%comspec% /k reg.exe EXPORT HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\RDPWD C:\regbkup\rdpserv.reg"
      WScript.Echo ("Completed Export " & Now)

      'Import registry keys
      WScript.Echo ("Beginning Import " & Now)
      objShell.Exec "%comspec% /k reg.exe IMPORT C:\fix.reg"
      WScript.Echo ("Completed Import " & Now)
End If
Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros