jeffg_91911
asked on
VB SCRIPT - setting remote registry entries
I need a simple script to setup a ODBC DNS. I got things working with .Regwrite for a local machine, but for a remote machine seems like I need to use SetStringValue.
In the following code lresult returns 2.
'Option Explicit
'Constants
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005
Dim RegObj
Dim SysEnv
sComputer = "optimist"
'Set theArgs = wscript.Arguments
'Wscript.echo theArgs(0)
Set RegObj = WScript.CreateObject("WScr ipt.Shell" )
'***** Specify the DSN parameters *****
DataSourceName = "Northwind"
DatabaseName = "C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\No rthwind.md b"
Description = "Northwind DB"
DriverName = "Microsoft Access Driver (*.mdb)"
sPath = "SOFTWARE\ODBC\ODBC.INI\" & DataSourceName
Set oRegistry = GetObject("winmgmts:{imper sonationLe vel=impers onate}//" & sComputer & "/root/default:StdRegProv" )
lResult = oRegistry.SetStringValue(H KEY_LOCAL_ MACHINE, sPath, "DBQ", DatabaseName)
lResult = oRegistry.SetStringValue(H KEY_LOCAL_ MACHINE, sPath, "Description", Descritption)
lResult = oRegistry.SetStringValue(H KEY_LOCAL_ MACHINE, sPath, "Driver", DriverName)
If (lResult = 0) And (Err.Number = 0) Then
msgbox("Success")
Else
msgbox("lResult = " & lResult & " Err.Number =" & Err.Number)
End If
'MsgBox DataSourceName & " DSN Created!"
Set RegObj = Nothing
Set SysEnv = Nothing
In the following code lresult returns 2.
'Option Explicit
'Constants
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005
Dim RegObj
Dim SysEnv
sComputer = "optimist"
'Set theArgs = wscript.Arguments
'Wscript.echo theArgs(0)
Set RegObj = WScript.CreateObject("WScr
'***** Specify the DSN parameters *****
DataSourceName = "Northwind"
DatabaseName = "C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\No
Description = "Northwind DB"
DriverName = "Microsoft Access Driver (*.mdb)"
sPath = "SOFTWARE\ODBC\ODBC.INI\" & DataSourceName
Set oRegistry = GetObject("winmgmts:{imper
lResult = oRegistry.SetStringValue(H
lResult = oRegistry.SetStringValue(H
lResult = oRegistry.SetStringValue(H
If (lResult = 0) And (Err.Number = 0) Then
msgbox("Success")
Else
msgbox("lResult = " & lResult & " Err.Number =" & Err.Number)
End If
'MsgBox DataSourceName & " DSN Created!"
Set RegObj = Nothing
Set SysEnv = Nothing
ASKER
Smallint your script works well!
Can you help me out. I'm trying to add a key to the ODBC Data Sources. This is needed so it shows up in the ODBC DSN gui.
You have already well deseerved the 500 pts A+
Option Explicit
Dim oShell
Dim sRemoteComputer
Dim DataSourceName
Dim DatabaseName
Dim Description
Dim DriverName
Dim sPath
Dim dsPath
Dim strResult
Dim Driver
function CapturingCMDInfo(strComman d)
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
'***** Specify remote computer *****
'sRemoteComputer = "\\geddesarmini"
sRemoteComputer = "\\optimist"
'***** Specify the DSN parameters *****
DataSourceName = "Northwind"
DatabaseName = "C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\No rthwind.md b"
Description = "Northwind DB"
DriverName = "Microsoft Access Driver (*.mdb)"
sPath = "\HKLM\SOFTWARE\ODBC\ODBC. INI\" & DataSourceName
dsPath = "\HKLM\SOFTWARE\ODBC\ODBC. INI\ODBC Data Sources\Northwind"
Driver = "C:\Windows\System32\odbcj t32.dll"
Set oShell= WScript.CreateObject("WScr ipt.Shell" )
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & sPath &" /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed1!!!")
Set oShell = Nothing
WScript.quit
End If
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & sPath &" /v DBQ /t REG_SZ /d " & chr(34) & DatabaseName & chr(34) & " /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed2!!!")
Set oShell = Nothing
WScript.quit
End If
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & sPath &" /v Description /t REG_SZ /d " & chr(34) & Description & chr(34)& " /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed3!!!")
Set oShell = Nothing
WScript.quit
End If
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & sPath &" /v Driver /t REG_SZ /d " & chr(34) & Driver & chr(34) & " /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed4!!!")
Set oShell = Nothing
WScript.quit
End If
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & sPath &" /v DriverID /t REG_DWORD /d " & chr(34) & 25 & chr(34) & " /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed5!!!")
Set oShell = Nothing
WScript.quit
End If
'This is the part that is messed up
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & dsPath &" /v DriverName /t REG_SZ /d " & chr(34) & DriverName & chr(34) & " /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed6!!!")
Set oShell = Nothing
WScript.quit
End If
msgbox("Success!!!")
Set oShell = Nothing
Can you help me out. I'm trying to add a key to the ODBC Data Sources. This is needed so it shows up in the ODBC DSN gui.
You have already well deseerved the 500 pts A+
Option Explicit
Dim oShell
Dim sRemoteComputer
Dim DataSourceName
Dim DatabaseName
Dim Description
Dim DriverName
Dim sPath
Dim dsPath
Dim strResult
Dim Driver
function CapturingCMDInfo(strComman
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
'***** Specify remote computer *****
'sRemoteComputer = "\\geddesarmini"
sRemoteComputer = "\\optimist"
'***** Specify the DSN parameters *****
DataSourceName = "Northwind"
DatabaseName = "C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\No
Description = "Northwind DB"
DriverName = "Microsoft Access Driver (*.mdb)"
sPath = "\HKLM\SOFTWARE\ODBC\ODBC.
dsPath = "\HKLM\SOFTWARE\ODBC\ODBC.
Driver = "C:\Windows\System32\odbcj
Set oShell= WScript.CreateObject("WScr
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & sPath &" /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed1!!!")
Set oShell = Nothing
WScript.quit
End If
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & sPath &" /v DBQ /t REG_SZ /d " & chr(34) & DatabaseName & chr(34) & " /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed2!!!")
Set oShell = Nothing
WScript.quit
End If
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & sPath &" /v Description /t REG_SZ /d " & chr(34) & Description & chr(34)& " /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed3!!!")
Set oShell = Nothing
WScript.quit
End If
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & sPath &" /v Driver /t REG_SZ /d " & chr(34) & Driver & chr(34) & " /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed4!!!")
Set oShell = Nothing
WScript.quit
End If
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & sPath &" /v DriverID /t REG_DWORD /d " & chr(34) & 25 & chr(34) & " /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed5!!!")
Set oShell = Nothing
WScript.quit
End If
'This is the part that is messed up
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & dsPath &" /v DriverName /t REG_SZ /d " & chr(34) & DriverName & chr(34) & " /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed6!!!")
Set oShell = Nothing
WScript.quit
End If
msgbox("Success!!!")
Set oShell = Nothing
Your full script. Of course will only work for Access ODBC Data Sources.
Cheers
Option Explicit
Dim oShell
Dim sRemoteComputer
Dim DataSourceName
Dim DatabaseName
Dim Description
Dim DriverName
Dim sRootPath
Dim sDriverPath
function CapturingCMDInfo(strComman d)
dim oExec
'msgbox strCommand
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 SetRemoteRegValue(sRemoteC omputer,sP ath,sKey,s DataType,s Data)
Dim sResult
Select case sDataType
case ""
sResult = CapturingCMDInfo("REG ADD " & chr(34) & sRemoteComputer & sPath & chr(34) &" /f")
case "REG_SZ"
sResult = CapturingCMDInfo("REG ADD " & chr(34) & sRemoteComputer & sPath & chr(34) &" /v " & sKey & " /t REG_SZ /d " & chr(34) & sData & chr(34) & " /f")
case "REG_DWORD"
sResult = CapturingCMDInfo("REG ADD " & chr(34) & sRemoteComputer & sPath & chr(34) &" /v " & sKey & " /t REG_DWORD /d " & sData & " /f")
'This case is not complete because there are more key types... but enough for our purposes
end select
If instr (sResult, "operation completed successfully") = 0 Then
msgbox("Failed Setting " & sPath & "\" & sKey & "!!!" )
Set oShell = Nothing
WScript.quit
end if
end Sub
'***** Specify remote computer *****
sRemoteComputer = "\\optimist"
Set oShell= WScript.CreateObject("WScr ipt.Shell" )
'***** Specify the DSN parameters *****
DataSourceName = "Northwind"
DatabaseName = "C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\No rthwind.md b"
Description = "Northwind DB"
sRootPath = "\HKLM\SOFTWARE\ODBC\ODBC. INI\"
'Be carefully because is language dependant
DriverName = "Microsoft Access Driver (*.mdb)"
'Firts of all let's find where Access Driver is located, because is installation dependant
sDriverPath = oShell.RegRead("HKLM\SOFTW ARE\ODBC\O DBCINST.IN I\" & DriverName & "\Driver")
'Here we go!
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & "ODBC Data Sources" ,"TestKK","REG_SZ",DriverN ame)
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & DataSourceName,"","","")
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & DataSourceName,"DBQ","REG_ SZ",Databa seName)
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & DataSourceName,"Driver","R EG_SZ",sDr iverPath)
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & DataSourceName,"Descriptio n","REG_SZ ",Descript ion)
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & DataSourceName,"DriverId", "REG_DWORD ","25")
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & DataSourceName,"FIL","REG_ SZ","MS Access;")
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & DataSourceName,"SafeTransa ctions","R EG_DWORD", "0")
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & DataSourceName,"UID","REG_ SZ","")
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & DataSourceName & "\Engines","","","")
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & DataSourceName & "\Engines\Jet","","","")
'These values are by default when installing ODBC Access source, modify at your needings
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & DataSourceName & "\Engines\Jet","ImplicitCo mmitSync", "REG_SZ"," ")
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & DataSourceName & "\Engines\Jet","MaxBufferS ize","REG_ DWORD","20 48")
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & DataSourceName & "\Engines\Jet","PageTimeou t","REG_DW ORD","5")
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & DataSourceName & "\Engines\Jet","Threads"," REG_DWORD" ,"3")
call SetRemoteRegValue(sRemoteC omputer,sR ootPath & DataSourceName & "\Engines\Jet","UserCommit Sync","REG _SZ","Yes" )
msgbox("Success!!!")
Set oShell = Nothing
Cheers
Option Explicit
Dim oShell
Dim sRemoteComputer
Dim DataSourceName
Dim DatabaseName
Dim Description
Dim DriverName
Dim sRootPath
Dim sDriverPath
function CapturingCMDInfo(strComman
dim oExec
'msgbox strCommand
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 SetRemoteRegValue(sRemoteC
Dim sResult
Select case sDataType
case ""
sResult = CapturingCMDInfo("REG ADD " & chr(34) & sRemoteComputer & sPath & chr(34) &" /f")
case "REG_SZ"
sResult = CapturingCMDInfo("REG ADD " & chr(34) & sRemoteComputer & sPath & chr(34) &" /v " & sKey & " /t REG_SZ /d " & chr(34) & sData & chr(34) & " /f")
case "REG_DWORD"
sResult = CapturingCMDInfo("REG ADD " & chr(34) & sRemoteComputer & sPath & chr(34) &" /v " & sKey & " /t REG_DWORD /d " & sData & " /f")
'This case is not complete because there are more key types... but enough for our purposes
end select
If instr (sResult, "operation completed successfully") = 0 Then
msgbox("Failed Setting " & sPath & "\" & sKey & "!!!" )
Set oShell = Nothing
WScript.quit
end if
end Sub
'***** Specify remote computer *****
sRemoteComputer = "\\optimist"
Set oShell= WScript.CreateObject("WScr
'***** Specify the DSN parameters *****
DataSourceName = "Northwind"
DatabaseName = "C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\No
Description = "Northwind DB"
sRootPath = "\HKLM\SOFTWARE\ODBC\ODBC.
'Be carefully because is language dependant
DriverName = "Microsoft Access Driver (*.mdb)"
'Firts of all let's find where Access Driver is located, because is installation dependant
sDriverPath = oShell.RegRead("HKLM\SOFTW
'Here we go!
call SetRemoteRegValue(sRemoteC
call SetRemoteRegValue(sRemoteC
call SetRemoteRegValue(sRemoteC
call SetRemoteRegValue(sRemoteC
call SetRemoteRegValue(sRemoteC
call SetRemoteRegValue(sRemoteC
call SetRemoteRegValue(sRemoteC
call SetRemoteRegValue(sRemoteC
call SetRemoteRegValue(sRemoteC
call SetRemoteRegValue(sRemoteC
call SetRemoteRegValue(sRemoteC
'These values are by default when installing ODBC Access source, modify at your needings
call SetRemoteRegValue(sRemoteC
call SetRemoteRegValue(sRemoteC
call SetRemoteRegValue(sRemoteC
call SetRemoteRegValue(sRemoteC
call SetRemoteRegValue(sRemoteC
msgbox("Success!!!")
Set oShell = Nothing
Ops, bug, i went too fast. I was taking driver location from local computer!
change this line
sDriverPath = oShell.RegRead("HKLM\SOFTW ARE\ODBC\O DBCINST.IN I\" & DriverName & "\Driver")
with
sTemp = CapturingCMDInfo("REG QUERY " & chr(34) & sRemoteComputer & "\HKLM\SOFTWARE\ODBC\ODBCI NST.INI\" & DriverName & chr(34) &" /v Driver")
aTemp = Split(sTemp,"REG_SZ")
if Ubound(aTemp) <> 1 then
Msgbox "Failed querying Access Driver Location"
wscript.quit
end if
sDriverPath = mid(trim(aTemp(1)),1,Len(t rim(aTemp( 1)))-4)
change this line
sDriverPath = oShell.RegRead("HKLM\SOFTW
with
sTemp = CapturingCMDInfo("REG QUERY " & chr(34) & sRemoteComputer & "\HKLM\SOFTWARE\ODBC\ODBCI
aTemp = Split(sTemp,"REG_SZ")
if Ubound(aTemp) <> 1 then
Msgbox "Failed querying Access Driver Location"
wscript.quit
end if
sDriverPath = mid(trim(aTemp(1)),1,Len(t
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Cheers
Option Explicit
Dim oShell
Dim sRemoteComputer
Dim DataSourceName
Dim DatabaseName
Dim Description
Dim DriverName
Dim sPath
Dim strResult
function CapturingCMDInfo(strComman
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
'***** Specify remote computer *****
sRemoteComputer = "\\geddesarmini"
'***** Specify the DSN parameters *****
DataSourceName = "Northwind"
DatabaseName = "C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\No
Description = "Northwind DB"
DriverName = "Microsoft Access Driver (*.mdb)"
sPath = "\HKLM\SOFTWARE\ODBC\ODBC.
Set oShell= WScript.CreateObject("WScr
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & sPath &" /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed!!!")
Set oShell = Nothing
WScript.quit
End If
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & sPath &" /v DBQ /t REG_SZ /d " & chr(34) & DatabaseName & chr(34) & " /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed!!!")
Set oShell = Nothing
WScript.quit
End If
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & sPath &" /v Description /t REG_SZ /d " & chr(34) & Description & chr(34)& " /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed!!!")
Set oShell = Nothing
WScript.quit
End If
strResult = CapturingCMDInfo("REG ADD " & sRemoteComputer & sPath &" /v Driver /t REG_SZ /d " & chr(34) & DriverName & chr(34) & " /f")
If instr (strResult, "operation completed successfully") = 0 Then
msgbox("Failed!!!")
Set oShell = Nothing
WScript.quit
End If
msgbox("Success!!!")
Set oShell = Nothing