ODBC DSN 자동으로 등록하기

  • Post author:
  • Post category:
  • Post comments:0 Comments
  • Post last modified:February 8, 2020

로컬 컴퓨터에서 개발 중인 소프트웨어를 테스트하려고 하니, ODBC 설정을 자동으로 해줘야 해서 작성한 VB 스크립트다. 사실 거의 다 Understanding ODBC DSN creation에서 가져온 코드이고, 데이터 소스 이름과 데이터베이스 이름을 명령줄 인자로 넘겨받게 고쳤을 뿐이다.

현재 로컬 컴퓨터엔 Sql Server 2005 Express Edition이 깔려 있기 때문에 ODBC 드라이버를 SQL Native Client으로 설정해놓았고, Windows 인증으로 로그인하게 만들어놨다. 하드 코딩되어 있는 부분이 많다.

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

'Variables
'On Error resume next
Dim Arg
Dim DataSourceName
Dim DatabaseName
Dim Description
Dim DriverPath
Dim LastUser
Dim Server
Dim Trusted_connection
Dim DriverName
Dim sPath
Dim sComputer
'Value assignment

If WScript.Arguments.Count <> 2 Then
	WScript.Echo "USAGES:  this.vbs DataSourceName, DatabaseName"
	WScript.Quit(0)
Else
	DataSourceName = WScript.Arguments(0)
	DatabaseName = WScript.Arguments(1)

	If DataSourceName = "" Or DatabaseName = "" Then
		WScript.Echo "USAGES:  this.vbs DataSourceName, DatabaseName"
		WScript.Quit(0)
	End If

	WScript.Echo "DataSourceName: " & DataSourceName
	WScript.Echo "DatabaseName: " & DatabaseName
End If

DriverPath = "C:\WINNT\System32\sqlncli.dll"
LastUser="sa"
Server="localhost"
Trusted_connection="Yes"
Description="ODBC DSN for the Database:" & DatabaseName
DriverName="SQL Native Client"
sPath		= "SOFTWARE\ODBC\ODBC.INI\" & DataSourceName
sComputer = "."

'Read and loop through the input file
If (0 = CreateRegKey(sComputer, HKEY_LOCAL_MACHINE, sPath)) Then
	SetRegKeyStrValue sComputer, HKEY_LOCAL_MACHINE, sPath, "Database", DatabaseName
	SetRegKeyStrValue sComputer, HKEY_LOCAL_MACHINE, sPath, "Description", Description
	SetRegKeyStrValue sComputer, HKEY_LOCAL_MACHINE, sPath, "Driver", DriverPath
	SetRegKeyStrValue sComputer, HKEY_LOCAL_MACHINE, sPath, "LastUser",LastUser
	SetRegKeyStrValue sComputer, HKEY_LOCAL_MACHINE, sPath, "Server",Server
	SetRegKeyStrValue sComputer, HKEY_LOCAL_MACHINE, sPath, "Trusted_Connection",Trusted_connection

	'Write in "ODBC Data Sources" Key to allow ODBC Manager list & manage the new DSN
	SetRegKeyStrValue sComputer, HKEY_LOCAL_MACHINE,  "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", DataSourceName , DriverName
	' MsgBox (sComputer & " DONE!")
Else
	' MsgBox (sComputer & " Failed!")
End If

WScript.Quit(0)



'Create RegKey Function

Function CreateRegKey (sComputer, hTree, sKey)
	Dim oRegistry
	Dim lResult

	Set oRegistry	= GetObject("winmgmts:{impersonationLevel=impersonate}//" & sComputer & "/root/default:StdRegProv")
	lResult = oRegistry.CreateKey(hTree, sPath)

	If (lResult = 0) And (Err.Number = 0) Then
		CreateRegKey = 0
	Else
		CreateRegKey = 1
		msgbox("Create Key " & sKey & " Failed")
	End If

	Set oRegistry = Nothing
End Function

'set RegKey Function

Function SetRegKeyStrValue (sComputer, hTree, sKey, sValueName, sValue)
	Dim oRegistry
	Dim lResult

	Set oRegistry	= GetObject("winmgmts:{impersonationLevel=impersonate}//" _
			& sComputer & "/root/default:StdRegProv")
	lResult = oRegistry.SetStringValue(hTree,   sKey,  sValueName,  sValue)

	If (lResult = 0) And (Err.Number = 0) Then
		SetRegKeyStrValue = 0
	Else
		SetRegKeyStrValue = 1
		msgbox("Set Value for " & sKey & " Failed")
	End If
	Set oRegistry = Nothing
End Function
Author Details
Kubernetes, DevSecOps, AWS, 클라우드 보안, 클라우드 비용관리, SaaS 의 활용과 내재화 등 소프트웨어 개발 전반에 도움이 필요하다면 도움을 요청하세요. 지인이라면 가볍게 도와드리겠습니다. 전문적인 도움이 필요하다면 저의 현업에 방해가 되지 않는 선에서 협의가능합니다.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments