用于创建ODBC连接的脚本

时间:2009-12-10 18:43:26

标签: windows scripting odbc sms sccm

我需要通过SMS / SCCM部署一些软件,软件要求在Windows中创建ODBC连接。我给出的连接信息需要用户名和密码。我有一个批处理脚本将连接信息导入注册表,但我不知道如何将用户名和密码放入。我想编写脚本或将其放在某种可分发的包中。< / p>

谢谢, -Mathew

4 个答案:

答案 0 :(得分:9)

您可以使用以下脚本:

%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

答案 1 :(得分:4)

以下是如何使用VBScriptPowerShellPerl直接编辑ODBC注册表设置的示例。

答案 2 :(得分:4)

这是我使用的脚本(jan.2015)。替换所有&lt;&gt;用你的名字:

@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

答案 3 :(得分: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