-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExport-DACPACs-GUI-V0.10.ps1
More file actions
96 lines (81 loc) · 3.49 KB
/
Export-DACPACs-GUI-V0.10.ps1
File metadata and controls
96 lines (81 loc) · 3.49 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
89
90
91
92
93
94
95
96
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Create form
$form = New-Object System.Windows.Forms.Form
$form.Text = "DACPAC Exporter"
$form.Size = New-Object System.Drawing.Size(600,400)
$form.StartPosition = "CenterScreen"
# SQL Server label and textbox
$labelServer = New-Object System.Windows.Forms.Label
$labelServer.Text = "SQL Server Instance:"
$labelServer.Location = New-Object System.Drawing.Point(10,20)
$labelServer.Size = New-Object System.Drawing.Size(150,20)
$form.Controls.Add($labelServer)
$textboxServer = New-Object System.Windows.Forms.TextBox
$textboxServer.Location = New-Object System.Drawing.Point(160,20)
$textboxServer.Size = New-Object System.Drawing.Size(400,20)
$form.Controls.Add($textboxServer)
# Output folder label and textbox
$labelFolder = New-Object System.Windows.Forms.Label
$labelFolder.Text = "Output Folder:"
$labelFolder.Location = New-Object System.Drawing.Point(10,60)
$labelFolder.Size = New-Object System.Drawing.Size(150,20)
$form.Controls.Add($labelFolder)
$textboxFolder = New-Object System.Windows.Forms.TextBox
$textboxFolder.Location = New-Object System.Drawing.Point(160,60)
$textboxFolder.Size = New-Object System.Drawing.Size(300,20)
$form.Controls.Add($textboxFolder)
$buttonBrowse = New-Object System.Windows.Forms.Button
$buttonBrowse.Text = "Browse"
$buttonBrowse.Location = New-Object System.Drawing.Point(470,60)
$buttonBrowse.Add_Click({
$folderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
if ($folderBrowser.ShowDialog() -eq "OK") {
$textboxFolder.Text = $folderBrowser.SelectedPath
}
})
$form.Controls.Add($buttonBrowse)
# Log output
$textboxLog = New-Object System.Windows.Forms.TextBox
$textboxLog.Multiline = $true
$textboxLog.ScrollBars = "Vertical"
$textboxLog.Location = New-Object System.Drawing.Point(10,100)
$textboxLog.Size = New-Object System.Drawing.Size(550,200)
$form.Controls.Add($textboxLog)
# Export button
$buttonExport = New-Object System.Windows.Forms.Button
$buttonExport.Text = "Export DACPACs"
$buttonExport.Location = New-Object System.Drawing.Point(230,320)
$buttonExport.Add_Click({
$server = $textboxServer.Text
$folder = $textboxFolder.Text
$logFile = Join-Path $folder "ExportDACPACsGUILog__$(Get-Date -Format 'yyyyMMdd_HHmmss').txt"
if (-not (Get-Module -ListAvailable -Name dbatools)) {
Install-Module -Name dbatools -Scope CurrentUser -Force
}
Import-Module dbatools
if (-not (Test-Path -Path $folder)) {
New-Item -ItemType Directory -Path $folder | Out-Null
}
"Export started at $(Get-Date)" | Out-File -FilePath $logFile
$databases = Get-DbaDatabase -SqlInstance $server | Where-Object { -not $_.IsSystemObject }
foreach ($db in $databases) {
try {
$dacpacPath = Join-Path -Path $folder -ChildPath "" #"$($db.Name).dacpac"
Export-DbaDacPackage -SqlInstance $server -Database $db.Name -Path $dacpacPath -ErrorAction Stop
$msg = "[$(Get-Date)] Exported $($db.Name) to $dacpacPath"
$msg | Out-File -FilePath $logFile -Append
$textboxLog.AppendText("$msg`r`n")
} catch {
$err = "[$(Get-Date)] Failed to export $($db.Name): $_"
$err | Out-File -FilePath $logFile -Append
$textboxLog.AppendText("$err`r`n")
}
}
$textboxLog.AppendText("Export completed at $(Get-Date)`r`n")
})
$form.Controls.Add($buttonExport)
# Show form
$form.Topmost = $true
$form.Add_Shown({ $form.Activate() })
[void]$form.ShowDialog()