Anda di halaman 1dari 5

Create/Update/Delete ODBC connection using the registry

We have just recently asked by a customer to come up with a simple method of quickly update more than 500 machines with multiple ODBC connections. That might be a hell-of-a headache for the IT guys The way we have decided to achieve that, was by running a vbscript (I admit that I forgot it even still exists). So I have found the basic idea in the
Guy! Blog Hey, Scripting

(thank you!) and have made some changes and additions.

So here it is in full (can be downloaded from here):

sub UpdateOdbc(Name, Server, Database, User, Password, Trusted)

'Updates 'Please note

an that

existing this script

System must run

ODBC with

SQL administrative

connection. privileges.

'Parameters: 'Name (string) The ODBC DSN Name

'Server (string) - The full database server name. Might be in Server\Instance format 'Database 'User 'Password (string) (string) (string) the the user The name password database used used to to name login login

'Trusted (boolean) - states that the connection is to be trusted connection. When set to True, both ' 'Usage: 'UpdateOdbc 'or 'UpdateOdbc "Because Connection", "Because Connection", "Because\sql2008", "Because\sql2008", "BecauseDB ", "", "BecauseDB", "sa", "jku!oi", False username and password are disregarded.

trusted: "", True

if OdbcNameExists(Name) DeleteOdbc CreateOdbc Name, Server, Database, User, Password,

= true then Name Trusted

end if end sub

sub CreateOdbc(Name, 'Creates 'Please note a that

Server, System this script

Database, ODBC must run

User,

Password, SQL

Trusted) connection. privileges.

with

administrative

'Parameters: 'Name (string) The ODBC DSN Name

'Server (string) - The full database server name. Might be in Server\Instance format 'Database 'User 'Password (string) (string) (string) the the user The name password database used used to to name login login

'Trusted (boolean) - states that the connection is to be trusted connection. When set to True, both ' username and password are disregarded.

'Usage: 'CreateOdbc 'or 'CreateOdbc "Because Connection", "Because Connection", "Because\sql2008", "Because\sql2008", = "BecauseDB", "", "BecauseDB", "sa", "jku!oi", False

trusted: "", True

Const HKEY_LOCAL_MACHINE

&H80000002

computer Set registryObject=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & computer

= "." _

& "\root\default:StdRegProv")

keyPath valueName = Name

= "SOFTWARE\ODBC\ODBC.INI\ODBC

Data

Sources"

'You '"SQL

may Server

want Native Native

to Client Client"

replace 10.0" for for

"SQL SQL SQL

Server" native Native client

with 10 client

'"SQL

value registryObject.SetStringValue

= "SQL

Server" HKEY_LOCAL_MACHINE,keyPath,valueName,value

keyPath registryObject.CreateKey keyPath valueName value registryObject.SetStringValue

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

Name HKEY_LOCAL_MACHINE,keyPath

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

Name = "Database"

Database HKEY_LOCAL_MACHINE,keyPath,valueName,value

'Driver 'Instead 'SQL 'SQL valueName value

of

Defaults

to SQLSRV32.dll Client

SQL you

Driver may 10 -

(SQLSRV32.dll) use: SQLNCLI.dll SQLNCLI10.dll = "Driver"

Native Server Native

Client

= "C:\WINDOWS\System32\SQLSRV32.dll" HKEY_LOCAL_MACHINE,keyPath,valueName,value = "Server" = Server HKEY_LOCAL_MACHINE,keyPath,valueName,value

registryObject.SetStringValue valueName value registryObject.SetStringValue

if Trusted valueName value registryObject.SetStringValue else valueName value registryObject.SetStringValue =

= True then = "Trusted_Connection" = "Yes" HKEY_LOCAL_MACHINE,keyPath,valueName,value

= "Password" Password HKEY_LOCAL_MACHINE,keyPath,valueName,value

valueName value =

= "PWD" Password

registryObject.SetStringValue

HKEY_LOCAL_MACHINE,keyPath,valueName,value

valueName value registryObject.SetStringValue =

= "username" User

HKEY_LOCAL_MACHINE,keyPath,valueName,value

valueName value registryObject.SetStringValue end if end sub =

= "LastUser" User

HKEY_LOCAL_MACHINE,keyPath,valueName,value

sub DeleteOdbc(Name) 'Use 'Please ' 'Parameters: 'Name ' 'Usage: 'DeleteOdbc Const HKEY_LOCAL_MACHINE computer Set registryObject=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & computer keyPath registryObject.DeleteKey "Because = Connection" &H80000002 = "." _ (string) The ODBC DSN Name note to that this delete script must an run with ODBC administrative Connection privileges.

& "\root\default:StdRegProv") = "SOFTWARE\ODBC\ODBC.INI\" & HKEY_LOCAL_MACHINE, Name keyPath

keyPath valueName

= "SOFTWARE\ODBC\ODBC.INI\ODBC =

Data

Sources" Name

registryObject.DeleteValue end sub

HKEY_LOCAL_MACHINE,keyPath,valueName

function OdbcNameExists(Name) Const HKEY_LOCAL_MACHINE computer Set registryObject=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & computer keyPath registryObject.GetStringValue = &H80000002 = "." _

& "\root\default:StdRegProv") = "SOFTWARE\ODBC\ODBC.INI\" & HKEY_LOCAL_MACHINE, keyPath, "Server", Name key

if IsNull(key) then OdbcNameExists else OdbcNameExists end if end function = true = false

Published May 12 2010, 08:58 AM by Amit Peer Attachment: ChangeODBC.zip

Comments

Anda mungkin juga menyukai