ODBC scripts
- Fix and optimize export script for ODBC DSNs, registry keys, and driver inventory. - Fix bug in the restore ODBC DSNs and registry keys scripts, ensuring compatibility with different Windows architectures.
This commit is contained in:
115
restore-ODBC.ps1
Normal file
115
restore-ODBC.ps1
Normal file
@@ -0,0 +1,115 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Restore ODBC DSNs and registry keys, and identify missing drivers.
|
||||
|
||||
.DESCRIPTION
|
||||
- Extracts ODBC_Backup.zip into C:\ODBC_Migration\restore
|
||||
- Imports all .reg and .xml files
|
||||
- Recreates DSNs
|
||||
- Compares exported vs installed ODBC drivers
|
||||
- Displays missing drivers with official vendor download links
|
||||
- Logs all operations
|
||||
#>
|
||||
|
||||
# ===========================
|
||||
# CONFIGURATION
|
||||
# ===========================
|
||||
$BaseDir = "C:\ODBC_Migration"
|
||||
$ZipFile = Join-Path $BaseDir "ODBC_Backup.zip"
|
||||
$ExtractDir = Join-Path $BaseDir "restore"
|
||||
$LogFile = Join-Path $BaseDir ("restore_log_" + (Get-Date -Format "yyyyMMdd_HHmm") + ".txt")
|
||||
|
||||
# ===========================
|
||||
# PREP
|
||||
# ===========================
|
||||
New-Item -ItemType Directory -Path $ExtractDir -Force | Out-Null
|
||||
Start-Transcript -Path $LogFile -Append
|
||||
Write-Host "`n=== ODBC RESTORE STARTED ===`n" -ForegroundColor Cyan
|
||||
|
||||
# ===========================
|
||||
# STEP 1: EXTRACT ZIP PACKAGE
|
||||
# ===========================
|
||||
if (-not (Test-Path $ZipFile)) {
|
||||
Write-Error "Cannot find ODBC_Backup.zip in $BaseDir"
|
||||
Stop-Transcript
|
||||
exit 1
|
||||
}
|
||||
Write-Host "Extracting archive..."
|
||||
Expand-Archive -Path $ZipFile -DestinationPath $ExtractDir -Force
|
||||
|
||||
# ===========================
|
||||
# STEP 2: IMPORT REGISTRY KEYS
|
||||
# ===========================
|
||||
Write-Host "Importing registry keys..."
|
||||
$regFiles = Get-ChildItem $ExtractDir -Filter *.reg
|
||||
foreach ($reg in $regFiles) {
|
||||
Write-Host " -> $($reg.Name)"
|
||||
reg import $reg.FullName | Out-Null
|
||||
}
|
||||
|
||||
# ===========================
|
||||
# STEP 3: RESTORE DSNs
|
||||
# ===========================
|
||||
$xmlPath = Join-Path $ExtractDir "odbc_dsns.xml"
|
||||
if (Test-Path $xmlPath) {
|
||||
Write-Host "Recreating DSNs from XML..."
|
||||
$dsns = Import-Clixml $xmlPath
|
||||
foreach ($dsn in $dsns) {
|
||||
try {
|
||||
Add-OdbcDsn @dsn -ErrorAction Stop
|
||||
Write-Host " + Added DSN: $($dsn.Name)" -ForegroundColor Green
|
||||
} catch {
|
||||
Write-Warning " ! Failed to add DSN: $($dsn.Name) - $_"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Write-Warning "No odbc_dsns.xml found in package."
|
||||
}
|
||||
|
||||
# ===========================
|
||||
# STEP 4: VERIFY DRIVER PRESENCE
|
||||
# ===========================
|
||||
Write-Host "`nChecking for missing ODBC drivers..." -ForegroundColor Yellow
|
||||
$exportCsv = Join-Path $ExtractDir "odbc_driver_product_map.csv"
|
||||
if (-not (Test-Path $exportCsv)) {
|
||||
Write-Warning "No driver map CSV found — skipping driver comparison."
|
||||
} else {
|
||||
$needed = Import-Csv $exportCsv
|
||||
$installed = Get-OdbcDriver | Select-Object -ExpandProperty Name
|
||||
|
||||
$missing = @()
|
||||
foreach ($n in $needed) {
|
||||
if ($n.OdbcDriverName -and ($installed -notcontains $n.OdbcDriverName)) {
|
||||
$missing += $n
|
||||
}
|
||||
}
|
||||
|
||||
if ($missing.Count -gt 0) {
|
||||
Write-Host "`n=== Missing ODBC Drivers Detected ===`n" -ForegroundColor Red
|
||||
foreach ($m in $missing) {
|
||||
$dl = $m.DownloadURL
|
||||
if ($dl -match '^https?') {
|
||||
Write-Host (" - {0} ({1})`n Download: {2}" -f $m.OdbcDriverName, $m.Platform, $dl) -ForegroundColor Yellow
|
||||
} else {
|
||||
Write-Host (" - {0} ({1})`n Download: [Manual search required]" -f $m.OdbcDriverName, $m.Platform) -ForegroundColor DarkYellow
|
||||
}
|
||||
}
|
||||
Write-Host "`nSee the detailed Markdown report for clickable links:" -ForegroundColor Cyan
|
||||
Write-Host " $($ExtractDir)\ODBC_Migration_Report.md`n" -ForegroundColor White
|
||||
} else {
|
||||
Write-Host "All previously detected drivers are installed." -ForegroundColor Green
|
||||
}
|
||||
}
|
||||
|
||||
# ===========================
|
||||
# STEP 5: SUMMARY
|
||||
# ===========================
|
||||
Write-Host "`n=== RESTORE COMPLETE ===`n" -ForegroundColor Cyan
|
||||
Write-Host "✔ Registry and DSNs restored"
|
||||
Write-Host "✔ Logs saved to: $LogFile"
|
||||
Write-Host "✔ Extracted files in: $ExtractDir"
|
||||
Write-Host "`nNext Steps:"
|
||||
Write-Host "1️⃣ Verify DSNs in ODBC Administrator (32-bit and 64-bit)."
|
||||
Write-Host "2️⃣ Install any missing drivers listed above."
|
||||
Write-Host "3️⃣ Re-run this script if needed after driver installation.`n"
|
||||
Stop-Transcript
|
||||
Reference in New Issue
Block a user