• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5984
  • Last Modified:

How to delete registry key.. VB script

Hi

Creating registry keys using the script from  http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21325149.html

I now need a VB script to delete it.

I'm trying this but it does not seem to work.

Option Explicit

Dim strComputer
Dim oReg
Dim strKeyPath
Dim strValueName

const HKEY_CURRENT_USER = &H80000001
'Set objArgs = WScript.Arguments
'strValueName = objArgs(0)

strComputer = "optimist"

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = "\HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI"
strValueName = "Northwind"
 
oReg.DeleteValue HKEY_CURRENT_USER,strKeyPath,strValueName



Any suggestions

Thanks

- Jeff
0
jeffg_91911
Asked:
jeffg_91911
  • 2
  • 2
1 Solution
 
SmallintCommented:
Here you have.

Cheers


Option Explicit

Dim oShell
Dim sRemoteComputer
Dim DataSourceName
Dim sRootPath


function CapturingCMDInfo(strCommand)

     dim oExec
     
     CapturingCMDInfo= "" 
     Set oExec = oShell.Exec (strCommand)

     Do While Not oExec.StdOut.AtEndOfStream
         CapturingCMDInfo = oExec.StdOut.ReadAll()
          If Instr(CapturingCMDInfo, "out") > 0 Then
               Exit Do
          End If
     Loop

end function

Sub DeleteRemoteRegValue(sRemoteComputer,sPath,sKey)

     Dim sResult

     if skey = "" then          
           sResult = CapturingCMDInfo("REG DELETE " & chr(34) & sRemoteComputer & sPath & chr(34) &" /f")
     else
      sResult = CapturingCMDInfo("REG DELETE " & chr(34) & sRemoteComputer & sPath & chr(34) & " /v "& sKey & " /f")
     end if            
         

     If instr (sResult, "operation completed successfully") = 0 Then
          msgbox("Failed Deleting " & sPath & "\" & sKey & "!!!" )
          Set oShell = Nothing
          WScript.quit
     end if


end Sub

'***** Specify remote computer *****
sRemoteComputer = "\\maverick"
 
Set oShell= WScript.CreateObject("WScript.Shell")
     
'***** Specify the DSN parameters *****
DataSourceName = "Northwind"
sRootPath = "\HKLM\SOFTWARE\ODBC\ODBC.INI\"


call DeleteRemoteRegValue(sRemoteComputer,sRootPath & "ODBC Data Sources" , DataSourceName)
call DeleteRemoteRegValue(sRemoteComputer,sRootPath & DataSourceName,"" )

msgbox("Success!!!")

Set oShell = Nothing
0
 
jeffg_91911Author Commented:
Excellet. thanks so much.
0
 
SmallintCommented:
Glad to help. Thanks to you for points. :))
0
 
jeffg_91911Author Commented:
I have a follow up queston to this.
I will Post it as a new question with the subject "VB Script How to remotely delete multiple odbc connections."

Thanks

- Jeff
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now