Monday, February 15, 2010

Create 32-Bit system ODBC DSN with Powershell

This Powershell script creates a 32-bit system DSN on a 64-bit server. I got tired of manually creating DSNs for my VMware vCenter installations, so I automated the process. The only argument you need to pass to the script is the FQDN or IP address of your SQL server. If you don't pass any arguments, the script will abort with an error message.

I apologize for the line wraps, but I think its fairly obvious where the breaks are. You can change the $DSNName to be any name that you wish. $DBName is the name of the database on the SQL server. The script assumes the SQL 2008 native client is installed. If you use a different client you will need to modify the script a bit.

## Creates a 32-bit System DSN on 64-bit OS.

$DSNName = "vCenter Server Update Manager"
$DBName = "vCenter Update Manager"

If($args[0] -eq $NULL) { echo "Must specify FQDN or IP of SQL server."; Exit}

$HKLMPath1 = "HKLM:\SOFTWARE\Wow6432Node\ODBC\ODBC.INI\" + $DSNName

$HKLMPath2 = "HKLM:\SOFTWARE\Wow6432Node\ODBC\ODBC.INI\ODBC Data Sources"

md $HKLMPath1 -ErrorAction silentlycontinue

set-itemproperty -path $HKLMPath1 -name Driver -value "C:\WINDOWS\SysWOW64\sqlncli10.dll"

set-itemproperty -path $HKLMPath1 -name Description -value $DSNName
set-itemproperty -path $HKLMPath1 -name Server -value $args[0]
set-itemproperty -path $HKLMPath1 -name LastUser -value ""
set-itemproperty -path $HKLMPath1 -name Trusted_Connection -value "Yes"
set-itemproperty -path $HKLMPath1 -name Database -value $DBName

## This is required to allow the ODBC connection to show up in the ODBC Administrator application.

md $HKLMPath2 -ErrorAction silentlycontinue

set-itemproperty -path $HKLMPath2 -name "$DSNName" -value "SQL Server Native Client 10.0"

4 comments:

  1. Thank you much for sharing your script! I'll check it out.
    -ngan

    ReplyDelete
  2. Hi Derek, how do we save the password using this method when we do a SQL Authentication. I used the LastUSer parameter to have the UserID but was unable to do the password.

    ReplyDelete
  3. I only use Windows authentication for security reasons, so I don't have any experience with SQL authentication parameters.

    ReplyDelete
  4. Derek,
    How can I use it under VBA using a button on an Access form?

    Thanks!

    ReplyDelete