-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractDACPACsFromDatabaseProject.psm1
More file actions
88 lines (72 loc) · 3.18 KB
/
ExtractDACPACsFromDatabaseProject.psm1
File metadata and controls
88 lines (72 loc) · 3.18 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# This module provides functions to read a Publish Profile from a database project, and export all DACPACs from the SQL Server instance.
# Requires dbatools module
function Connect-FromPublishProfile {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$PublishProfilePath
)
if (-not (Test-Path $PublishProfilePath)) {
throw "The specified publish profile path does not exist: $PublishProfilePath"
}
try {
[xml]$xml = Get-Content -Path $PublishProfilePath
$connectionString = $xml.Project.PropertyGroup.TargetConnectionString
$serverName = if ($connectionString) {
$connectionString -replace '.*Data Source=([^;]+);.*', '$1'
} else {
$xml.Project.PropertyGroup.TargetServerName
}
$databaseName = $xml.Project.PropertyGroup.TargetDatabaseName
if (-not $serverName -or -not $databaseName) {
throw "Could not extract server or database name from the publish profile."
}
Write-Verbose "Connecting to SQL Server instance: $serverName"
$instance = Connect-DbaInstance -SqlInstance $serverName
Write-Verbose "Connected. Target database: $databaseName"
return $instance
}
catch {
throw "Failed to connect using publish profile: $_"
}
}
#TODO: Update the Export-AllDacpacs function to use the Connect-FromPublishProfile function
# Export DACPACs from all user databases on a SQL Server instance
#TODO Update the path handling to match Export-DACPACs.ps1
# Requires dbatools module
function Export-AllDacpacs {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$SqlInstance,
[Parameter(Mandatory = $true)]
[string]$OutputFolder
)
# Usage: Export-AllDacpacs -SqlInstance "localhost" -OutputFolder "C:\Temp\Export-DACPACs"
# 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
}
# Create log file
$logFile = Join-Path -Path $OutputFolder -ChildPath "ExportLog_$(Get-Date -Format 'yyyyMMdd_HHmmss').txt"
"Export started at $(Get-Date)" | Out-File -FilePath $logFile
# Get user databases and 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 $SqlInstance -Database $db.Name -Path $dacpacPath -ErrorAction Stop
"[$(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
}
}
"Export completed at $(Get-Date)" | Out-File -FilePath $logFile -Append
Write-Host "Export complete. Log saved to $logFile"
}