Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating registry Keys/Values 2

Status
Not open for further replies.

silver2kgt

Technical User
Nov 18, 2003
19
0
0
US
I am trying to add ODBC connections on remote PC's for performance logging. I can setup the perfmon settings fine, I just can't seem to get the registry keys and values to write to setup the ODBC connection. Maybe I've spent too much time looking at it and am missing something, but my code looks like everything I have found on the net and it doesn't want to work for me. I'm running XP Pro SP2.

PS - Set oReg = ... is all on one line.
Code:
Const HKEY_LOCAL_MACHINE = &H80000002
Dim DSN, DBName, Description, DrivekeyPath, DriverName, Server, oReg, keyPath, keyPath2, strName

strName = "."
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strName & "\root\default:StdRegProv")
DSN = "PerfMon"
DBName = "PerfMon"
Description = "ODBC Connection for Performance Monitoring"
DrivekeyPath = "%SystemRoot%/system32/sqlsrv32.dll"
DriverName = "SQL Server"
Server = "test"
keyPath = "\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources\"
keyPath2 = "\SOFTWARE\ODBC\ODBC.INI\" & DSN

oReg.CreateKey HKEY_LOCAL_MACHINE, keyPath2
oReg.SetStringValue HKEY_LOCAL_MACHINE, keyPath, DSN, DriverName
oReg.SetStringValue HKEY_LOCAL_MACHINE, keyPath2, "Database", DBName
oReg.SetStringValue HKEY_LOCAL_MACHINE, keyPath2, "Description", Description
oReg.SetStringValue HKEY_LOCAL_MACHINE, keyPath2, "Driver", DrivekeyPath
oReg.SetStringValue HKEY_LOCAL_MACHINE, keyPath2, "Server", Server
 
I confess I am feeling lazy and won't extract the bit out that you need, but here is a script I wrote to make an ODBC connection and perform some tasks around that connection.

Code:
'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalSCRIPT(TM)
'
' NAME: AppEnablerCreateODBC.vbs
'
' AUTHOR: Mark D. MacLachlan, The Spider's Parlor
' URL:  http;//[URL unfurl="true"]www.thespidersparlor.com[/URL]
' DATE  : 11/3/2005
'
' COMMENT: Script will check to see if it has been run on computer previously
'			If it has, it will not run.  Script adds desktop icon For
'			App Enabler on Desktop and Startup folder.  Also sets ODBC settings
'			so App Enabler can work.  Also copies file to link with Active
'			Directory.  Adds Registry key HKLM\Software\CompanyName\Installs\AppEnabler\1
'==========================================================================

Set WshShell = WScript.CreateObject("WScript.Shell")
On Error Resume Next
regpath = "HKLM\SOFTWARE\CompanyName\Installs\"
NautInstall = (WSHShell.Regread(regpath & "AppEnabler"))
 If NautInstall = "1" Then
 	WScript.Quit
 Else
   Dim DataSourceName
   Dim DatabaseName
   Dim dbDescription
   Dim DriverPath
   Dim DriverName
   Dim LastUser
   Dim Regional
   Dim Server

   Const SystemFolder= 1
   Dim fso
   Dim SysFolder
   Dim SysFolderPath

   Set fso = wscript.CreateObject("Scripting.FileSystemObject")
   Set SysFolder =fso.GetSpecialFolder(SystemFolder)
   SysFolderPath= SysFolder.Path

   'Specify the DSN parameters.

   DataSourceName = "Temp1"
   DatabaseName = "Temp1"
   dbDescription = ""
   DriverPath = SysFolderPath & "\sqlsrv32.dll"
   Server = "Temps"
   LastUser  = "DBUser"

   DriverName = "SQL Server"
   
   Dim RegEdPath
   RegEdPath= "HKLM\SOFTWARE\ODBC\ODBC.INI\" & DataSourceName & "\"
   
   WshShell.RegWrite  RegEdPath  , ""

   

   WshShell.RegWrite  RegEdPath & "Database" , DatabaseName
   WshShell.RegWrite  RegEdPath & "Description" , dbDescription
   WshShell.RegWrite  RegEdPath & "Driver" , DriverPath
   WshShell.RegWrite  RegEdPath & "LastUser" , LastUser
   WshShell.RegWrite  RegEdPath & "Server" , Server

   WshShell.RegWrite "HKLM\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources\" & DataSourceName , DriverName

WshShell.Run("odbcad32.exe")
WshShell.AppActivate "Microsoft ODBC Administrator"
WScript.Sleep 10
WshShell.Sendkeys "+{tab}"
WshShell.AppActivate "Microsoft ODBC Administrator"
WshShell.Sendkeys "{right}"
WshShell.AppActivate "Microsoft ODBC Administrator"
WshShell.Sendkeys "{tab}"
WshShell.AppActivate "Microsoft ODBC Administrator"
WshShell.Sendkeys "Temp1"
WshShell.AppActivate "Microsoft ODBC Administrator"
WshShell.Sendkeys "%c"
WshShell.AppActivate "Microsoft ODBC Administrator"
WshShell.Sendkeys "%n"
WshShell.AppActivate "Microsoft ODBC Administrator"
WshShell.Sendkeys "%p"
WshShell.AppActivate "Microsoft ODBC Administrator"
WshShell.Sendkeys "DBPassword"
WshShell.AppActivate "Microsoft ODBC Administrator"
WshShell.Sendkeys "%n"
WshShell.AppActivate "Microsoft ODBC Administrator"
WshShell.Sendkeys "%n"
WshShell.AppActivate "Microsoft ODBC Administrator"
WshShell.Sendkeys "{enter}"
WshShell.AppActivate "Microsoft ODBC Administrator"
WshShell.Sendkeys "{right}"
WshShell.AppActivate "Microsoft ODBC Administrator"
WshShell.Sendkeys "{enter}"
WshShell.AppActivate "Microsoft ODBC Administrator"
WshShell.Sendkeys "{enter}"

Call WSHShell.Run("cmd.exe /C copy \\bedrock\shared\DMLogin.htm C:\Progra~\Hyland\Desktop\Forms\ /y")

strFavs = WshShell.SpecialFolders("AllUsersDesktop") 
strshortcut = strFavs & "\App Enabler.lnk" 
If Not fso.FileExists(strshortcut) Then 
        SET oUrlLink = WshShell.CreateShortcut(strshortcut) 
        exePath = "C:\Program Files\Hyland\Application Enabler\AEClient.exe"
        oUrlLink.TargetPath = exePath
        oUrlLink.Arguments = Chr(34) & "\\bedrock\imagesoft$\AppEnabler.xml" & Chr(34)
        oUrlLink.WorkingDirectory = ""
        oUrlLink.Save 
End If 


strFavs = WshShell.SpecialFolders("AllUsersStartup") 
strshortcut = strFavs & "\App Enabler.lnk" 
If Not fso.FileExists(strshortcut) Then 
        SET oUrlLink = WshShell.CreateShortcut(strshortcut) 
        exePath = "C:\Program Files\Hyland\Application Enabler\AEClient.exe"
        oUrlLink.TargetPath = exePath
        oUrlLink.Arguments = Chr(34) & "\\bedrock\imagesoft$\AppEnabler.xml" & Chr(34)
        oUrlLink.WorkingDirectory = ""
        oUrlLink.Save 
End If 

WSHShell.RegWrite regpath & "AppEnabler","1","REG_DWORD"
End If

I hope you find this post helpful.

Regards,

Mark
 
>[tt]keyPath = "[COLOR=red yellow]\[/color]SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources\"[/tt]
[tt]keyPath = "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources\"[/tt]

>[tt]keyPath2 = "[COLOR=red yellow]\[/color]SOFTWARE\ODBC\ODBC.INI\" & DSN[/tt]
[tt]keyPath2 = "SOFTWARE\ODBC\ODBC.INI\" & DSN[/tt]
 
Thanks for the replies. Tsuji was right, you can't have the "\" with this method.
 
silver, take a look at my script too, you will note that I found a way to handle a password for the connection which is not supported by vbscript. I had to use Sendkeys to get it done.

I hope you find this post helpful.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top