开发者

Exporting ODBC System DSNs from a windows 2003 machine?

开发者 https://www.devze.com 2023-02-08 09:59 出处:网络
Is there a way to export all the ODBC System DSNs from a windows 2003 machine开发者_如何学运维? System DSN information is stored under the HKEY_LOCAL_MACHINE\\SOFTWARE\\ODBC\\ODBC.INI registry key. Yo

Is there a way to export all the ODBC System DSNs from a windows 2003 machine开发者_如何学运维?


System DSN information is stored under the HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI registry key. You could export that key to a .reg file and import on another machine.

UPDATE:

You can also do it programmatically. Here are a few examples:

http://www.codeproject.com/KB/database/DSNAdmin.aspx

http://support.microsoft.com/kb/110507

http://blogs.technet.com/b/heyscriptingguy/archive/2004/11/10/can-i-create-and-delete-a-dsn-using-a-script.aspx


I have just done this myself with a very simple bat script for 32bit ODBC sources

regedit /e c:\backup\odbc.reg "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ODBC\ODBC.INI"

and for the 64bit sources or if you are on a 32bit operating system:

regedit /e c:\backup\odbc.reg "HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI"

This backs up all of the DSN's however you could then specify the DNS you want.


System DSN's are stored in windows registry under HKLM\Software\ODBC\ODBC.INI node So if you export this node to a *.reg file and run this reg file on a target machine, it should work.

The only thing, this reg file will contain some file paths which maybe computer specific, eg c:\WINNT\System32\bla-bla-bla.dll includes WINNT folder which on target machine may be called like WINDOWS. So you will need to spend a bit time to make sure all paths in *.reg file are correct for target machine where you would finally import.


I wrote some Powershell functions for copying ODBC connections from one computer to another, they are posted (and kept updated) at:

http://powershell.com/cs/media/p/32510.aspx

# Usage:  
# $srcConfig = Get-OdbcConfig srcComputerName   
# Import-OdbcConfig trgComputerName $scrConfig  
# Only returns data when setting values  

function Get-OdbcConfig {  
param( $srcName )  
    if ( Test-Connection $srcName -Count 1 -Quiet ) {  
        # cycle through the odbc and odbc32 keys  
        $keys = "SOFTWARE\ODBC\ODBC.INI", "SOFTWARE\Wow6432Node\ODBC\ODBC.INI"  
        foreach ( $key in $keys ){  
            # open remote registry  
            $type = [Microsoft.Win32.RegistryHive]::LocalMachine  
            $srcReg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey( $type, $srcName )  
            $OdbcKey = $srcReg.OpenSubKey( $key )  
            # red through each key  
            foreach ( $oDrvr in $OdbcKey.GetSubKeyNames() ){  
                # form the key path  
                $sKey = $key + "\" + $oDrvr  
                $oDrvrKey = $srcReg.OpenSubKey( $sKey )  
                # cycle through each value, capture the key path, name, value and type  
                foreach ( $oDrvrVal in $oDrvrKey.GetValueNames() ) {  
                        $regObj = New-Object psobject -Property @{  
                        Path = $sKey  
                        Name = $oDrvrVal  
                        Value = $oDrvrKey.GetValue( $oDrvrVal )  
                        Type = $oDrvrKey.GetValueKind( $oDrvrVal )  
                    }  
                # dump each to the console  
                $regObj  
                }  
            }  
        }  
    }  
    # can't ping  
    else { Write-Host "$srcName offline" }  
}  

function Import-OdbcConfig {  
param( $trgName, $srcConfig )  
    if ( Test-Connection $trgName -Count 1 -Quiet ) {  
        # open remote registry  
        $type = [Microsoft.Win32.RegistryHive]::LocalMachine  
        $trgReg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey( $type, $trgName )  
        # sort out the key paths and cycle through each  
        $paths = $srcConfig | select -Unique Path  
        foreach ( $key in $paths ){  
            # check for the key and create it if it's not there  
            if ( ! $trgReg.OpenSubKey( $key.Path ) ) { $writeKey = $trgReg.CreateSubKey( $key.Path ) }  
            # open the path for writing ($true)  
            $trgKey = $trgReg.OpenSubKey( $key.Path, $true )  
            # cycle through each value, check to see if it exists, create it if it doesn't  
            foreach ( $oDrvr in $srcConfig | where { $_.Path -eq $key.Path } ) {  
                if ( ! $trgKey.GetValue( $oDrvr.Name ) ) {  
                    $oType = $oDrvr.Type  
                    $writeValue = $trgKey.SetValue( $oDrvr.Name, $oDrvr.Value, [Microsoft.Win32.RegistryValueKind]::$oType  )  
                    $objObj = new-object psobject -Property @{  
                        Path = $oDrvr.Path  
                        Name = $oDrvr.Name  
                        Value = $trgKey.GetValue( $oDrvr.Name )  
                        Type = $trgKey.GetValueKind( $oDrvr.Name )  
                    }  
                }  
            $objObj  
            }  
        }  
    }  
    # can't ping  
    else { Write-Host "$srcName offline" }  
} 

Using these functions together you can copy all of one computers ODBC connections to another:

$srcConfig = Get-OdbcConfig srcComputerName
Import-OdbcConfig trgComputerName $scrConfig

It's possible to include only your favorite ODBC connection by filtering on the path:

Import-OdbcConfig trgComputerName ( $scrKeys | where { $_.Path -eq "SOFTWARE\ODBC\ODBC.INI\GoodDatabase" } )

Or filtering out ODBC connections you don't like:

Import-OdbcConfig trgComputerName ( $scrKeys | where { $_.Path -ne "SOFTWARE\ODBC\ODBC.INI\DatabaseIHate" } )

Import-OdbcConfig only returns data when setting values or can't ping target, if there's nothing to create it won't say anything.


If you can't find the registrations there, depending on if they are User DSN/System DSN, they can very will be in:

[HKEY_USERS\"User SID(dont' look for this, it will be a long number)\Software\ODBC\ODBC.INI]

0

精彩评论

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

关注公众号