开发者

Script to create ODBC connection

I need to deploy some software through SMS/SCCM and the software requires that an ODBC connection be created in Windows. The connectio开发者_如何学Pythonn 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


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"

Script to create ODBC connection


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


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜