7

I need to deploy some software through SMS/SCCM and the software requires that an ODBC connection be created in Windows. The connection information I was given requires a user name and password. I have a batch script to import the connection information into the registry, however I don't know how to go about putting the user name and password in. I'd like to script this or put it in some kind of distributable package.

Thanks, -Mathew

Mathew
  • 317
  • 1
  • 3
  • 8

4 Answers4

11

You may use the follow script:

%WINDIR%\System32\odbcconf.exe CONFIGDSN "SQL Server" "DSN=ControltubProducao|Description=ControltubProducao|SERVER=10.23.22.18|Trusted_Connection=No|Database=Controltub"
%WINDIR%\SysWOW64\odbcconf.exe CONFIGDSN "SQL Server" "DSN=ControltubProducao|Description=ControltubProducao|SERVER=10.23.22.18|Trusted_Connection=No|Database=Controltub"

enter image description here

BOB
  • 273
  • 3
  • 9
Izaac Silva
  • 111
  • 1
  • 2
  • 3
    Wow, this question is so old it's like, a couple of jobs ago :-) Not even sure if I can test this to say if you're correct or not. – Mathew Nov 01 '12 at 16:15
5

Here are examples of how to directly edit the ODBC Registry settings with VBScript, PowerShell, or Perl.

matt wilkie
  • 17,268
  • 24
  • 80
  • 115
ewall
  • 27,179
  • 15
  • 70
  • 84
5

Here's the script I use (jan. 2015). Replace all <> with your names:

@echo off
cls

echo Configure user Data Sources for <ApplicationName>
echo.

set dsn_name=<OdbcLinkName>
set config_dsn=configdsn "SQL Server" "DSN=%dsn_name%|Server=<SqlServerName>|Database=<DatabaseName>|Trusted_Connection=yes"

%windir%\system32\odbcconf %config_dsn%
%windir%\syswow64\odbcconf %config_dsn%

echo Data Source "%dsn_name%" has been configured.
echo.
echo Done.
echo.
pause
Sylvain Rodrigue
  • 4,751
  • 5
  • 53
  • 67
0
Option Explicit

Dim strDSNName, strDriver, strServer, strLastUser, strRegional, strDatabase
strDSNName = "DSN NAME"
strDriver = "C:\WINDOWS\system32\SQLSRV32.dll"
strServer = "SERVER ADDRESS"
strLastUser = "USERNAME (PLACEHOLDER)"
strRegional = "Yes"
strDatabase = "DB NAME"

If MsgBox("ODBC configuration listed below is going to be added to current user." & vbcrlf & _
          "Do you want to continue?" & vbcrlf & _
          "DSN Name: " & strDSNName & vbcrlf & _
          "Server: " & strServer & vbcrlf & _
          "Database: " & strDatabase, vbInformation + vbYesNo, "Confirmation Required") = vbYes Then
Else
  MsgBox "Operation cancelled by user.", vbExclamation, "Aborted"
  Wscript.Quit
End If

const HKEY_CURRENT_USER = &H80000001
Dim strComputer, objReg, strKeyPath, strValueName, strValue
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources"
strValueName = strDSNName 
strValue = "SQL Server"
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

strKeyPath = "SOFTWARE\ODBC\ODBC.INI\" & strDSNName
objReg.CreateKey HKEY_CURRENT_USER,strKeyPath

strKeyPath = "SOFTWARE\ODBC\ODBC.INI\" & strDSNName

strValueName = "Database"
strValue = strDatabase
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

strValueName = "Driver"
strValue = strDriver
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

strValueName = "Server"
strValue = strServer
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

strValueName = "Regional"
strValue = strRegional
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

strValueName = "LastUser"
strValue = strLastUser
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

Set objReg = Nothing

If (Err.Number = 0) Then 
  MsgBox "ODBC added successfully!" & vbcrlf & _
         "Details are listed below:" & vbcrlf & _
         "DSN Name: " & strDSNName & vbcrlf & _
         "Server: " & strServer & vbcrlf & _
         "Database: " & strDatabase, vbInformation, "Succeed"
Else
  Wscript.Echo "Operation failed. Error = " & Err.Number
End If