有没有一种方法可以从Windows 2003机器中导出所有ODBC系统DSN?
有没有一种方法可以从Windows 2003机器中导出所有ODBC系统DSN?
系统 DSN 信息存储在 HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI
注册表项下。您可以将该键导出为 .reg 文件并导入到另一台机器上。
更新:
你也可以通过编程实现。以下是一些示例:
http://www.codeproject.com/KB/database/DSNAdmin.aspx
regedit /e c:\backup\odbc.reg "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ODBC\ODBC.INI"
如果你使用64位的源,或者你运行在32位的操作系统上:
regedit /e c:\backup\odbc.reg "HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI"
这将备份所有的DSN,但您可以指定您想要的DNS。
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" }
}
Import-OdbcConfig trgComputerName ( $scrKeys | where { $_.Path -eq "SOFTWARE\ODBC\ODBC.INI\GoodDatabase" } )
Import-OdbcConfig trgComputerName ( $scrKeys | where { $_.Path -ne "SOFTWARE\ODBC\ODBC.INI\DatabaseIHate" } )
系统DSN存储在Windows注册表的HKLM\Software\ODBC\ODBC.INI
节点下。因此,如果您将此节点导出为*.reg文件并在目标计算机上运行此reg文件,则应该可以正常工作。
唯一需要注意的是,此reg文件将包含某些可能是计算机特定的文件路径,例如c:\WINNT\System32\bla-bla-bla.dll
,其中包含了WINNT文件夹,而在目标计算机上可能被称为WINDOWS
。因此,您需要花费一些时间来确保*.reg文件中的所有路径对于最终导入的目标计算机是正确的。
如果您无法在那里找到注册信息,取决于它们是用户 DSN 还是系统 DSN,它们可能会出现在以下位置:
[HKEY_USERS\"User SID(不要查找此项,它将是一个长数字)\Software\ODBC\ODBC.INI]