-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExport-DACPACs.ps1
More file actions
59 lines (49 loc) · 2.29 KB
/
Export-DACPACs.ps1
File metadata and controls
59 lines (49 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Configuration
$serverName = "localhost" # Change to your SQL Server instance
$outputFolder = "C:\Temp\Export-DACPACs"
$appendDateToLog = $false # Set to $true if you want to append date to log file name
$debug = $true
# If $appendDateToLog is true, append date to log file name
if ($appendDateToLog) {
$logFile = Join-Path -Path $outputFolder -ChildPath "ExportDACPACsLog_$(Get-Date -Format 'yyyyMMdd')"
}
else {
$logFile = Join-Path -Path $outputFolder -ChildPath "ExportDACPACsLog.txt"
}
# Ensure dbatools is installed
if (-not (Get-Module -ListAvailable -Name dbatools)) {
Write-Host "Installing dbatools..."
Install-Module -Name dbatools -Scope CurrentUser -Force
}
Import-Module dbatools
# Ensure output folder exists
if (-not (Test-Path -Path $outputFolder)) {
New-Item -ItemType Directory -Path $outputFolder | Out-Null
}
# Initialize log file
"Export started at $(Get-Date)" | Out-File -FilePath $logFile
# Avoid the "The certificate chain was issued by an authority that is not trusted." error
# This is a workaround for self-signed certificates or untrusted connections
Set-DbatoolsInsecureConnection -SessionOnly
$sqlInstance = Connect-DbaInstance -SqlInstance $serverName #-TrustServerCertificate
if (-not $sqlInstance) {
"[$(Get-Date)] SQL Server instance $serverName not found." | Out-File -FilePath $logFile -Append
Write-Host "SQL Server instance $serverName not found. Exiting script."
exit 1
}
# Export DACPACs
$databases = Get-DbaDatabase -SqlInstance $sqlInstance | Where-Object { -not $_.IsSystemObject }
foreach ($db in $databases) {
try {
$dacpacPath = Join-Path -Path $outputFolder -ChildPath "" #"$($db.Name).dacpac"
Export-DbaDacPackage -SqlInstance $serverName -Database $db.Name -Path $dacpacPath -ErrorAction Ignore
"[$(Get-Date)] Exported $($db.Name) to $dacpacPath" | Out-File -FilePath $logFile -Append
} catch {
"[$(Get-Date)] Failed to export $($db.Name): $_" | Out-File -FilePath $logFile -Append
}
if ($debug) {
Write-Host "Debug mode is ON. Exiting foreach loop after first iteration."
}
}
"Export completed at $(Get-Date)" | Out-File -FilePath $logFile -Append
Write-Host "Export complete. Log saved to $logFile"