-
Notifications
You must be signed in to change notification settings - Fork 22
/
SMO_DDL.ps1
371 lines (336 loc) · 15.7 KB
/
SMO_DDL.ps1
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<#
.SYNOPSIS
SMO_DDL.ps1
.DESCRIPTION
Connects to the specified SQL Server, and generates DDL for database(s) listed. Default = 'ALL' = all user databases.
The generated DDL script(s) are placed in the specified output folder.
The login specified ('sa' in the examples below) must have permissions to access the databases.
The specified output folder must exist. Specifying '.' is allowed.
.EXAMPLE (Windows)
powershell .\SMO_DDL.ps1 -Databases 'All' -OutputFolder 'C:\mydir' -ServerName 'IP.ADDRESS.GOES.HERE' -UserName 'sa' -Password 'mypassword'
powershell .\SMO_DDL.ps1 -Databases 'mydb,yourdb,otherdb' -OutputFolder 'C:\mydir' -ServerName 'IP.ADDRESS.GOES.HERE' -UserName 'sa' -Password 'mypassword'
.EXAMPLE (Linux/Mac)
pwsh ./SMO_DDL.ps1 -Databases 'mydb,yourdb,otherdb' -OutputFolder '/tmp' -ServerName 'IP.ADDRESS.GOES.HERE' -Username 'sa' -Password 'mypassword'
.NOTES
This Powershell script requires SMO (SQL Server Management Objects) to be installed. On Windows, when SQL Server itself is installed, SMO is typically already
installed and the script should work.
If SQL Server is not installed, the script will try to install SMO, but this is not 100% guaranteed to work - YMMV. When the script installs SMO, it will take
longer than subsequent runs.
When running this script without proper SMO installation, there will be multiple error messages, including something like 'Cannot find type [Microsoft.SqlServer.Management.Smo.Server]'.
When SMO is installed correctly but the script fails to access the SQL Server, there will only be an error message like 'Cannot connect to SQL Server [...]'
Execution speed very much depends on (i) available resources on the host where the script is invoked from and (ii) the network proximity to the SQL Server
since SMO performs many client-server roundtrips. With a few 1000's objects, it could take from minutes to hours to run, depending on the above.
This script can be invoked individually as shown above. It is also invoked by Babelfish Compass, so if you want to make any changes, take a copy first.
Note that a new version of Babelfish Compass may overwrite the existing script in the Compass installation firectory.
On Linux, powershell for Linux needs to be installed first (https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-linux)
This script was successfully tested on Windows, Mac, and Amazon Linux.
#>
# parameter defaults
[CmdletBinding()]
Param (
[Parameter(Mandatory = $false)][String[]]$Databases = 'ALL',
[Parameter(Mandatory = $false)][String]$OutputFolder = '',
[Parameter(Mandatory = $false)][String]$ServerName = 'localhost',
[Parameter(Mandatory = $false)][String]$UserName = 'sa',
[Parameter(Mandatory = $false)][String]$Password = 'nosuchpassword',
[Parameter(Mandatory = $false)][String]$SMOOutputDir = 'smo_ddl_output',
[Parameter(Mandatory = $false)][String]$DDLTag = ''
)
$ThisProgram = "SMO_DDL.ps1"
Function Get-Prereqs {
$CurrentPSVersion = $PSVersionTable.PSVersion
[version]$DesiredPSVersion = '5.1.0.0'
If ($CurrentPSVersion -lt $DesiredPSVersion) {
Write-Output 'ERROR: WMF5.1 Not installed, see here for the installation file: https://www.microsoft.com/en-us/download/details.aspx?id=54616'
Exit 1
}
$PPPresent = Get-PackageProvider -Name 'Nuget' -Force -ErrorAction SilentlyContinue
If (-not $PPPresent) {
Write-Output 'INFO: INSTALL: Installing the NuGet package provider'
Try {
$Null = Install-PackageProvider -Name 'NuGet' -MinimumVersion '2.8.5' -Force -ErrorAction Stop
} Catch [System.Exception] {
Write-Output "ERROR: Failed to install NuGet package provider $_"
Exit 1
}
}
$PsRepPresent = Get-PSRepository -Name 'PSGallery' | Select-Object -ExpandProperty 'InstallationPolicy' -ErrorAction SilentlyContinue
If ($PsRepPresent -ne 'Trusted') {
Write-Output 'INFO: INSTALL: Setting PSGallery respository to trusted'
Try {
Set-PSRepository -Name 'PSGallery' -InstallationPolicy 'Trusted' -ErrorAction Stop
} Catch [System.Exception] {
Write-Output "ERROR: Failed to set PSGallery respository to trusted $_"
Exit 1
}
}
$ModPresent = Get-Module -Name 'SqlServer' -ListAvailable | Select-Object -ExpandProperty 'Version' | Select-Object -ExpandProperty 'Major'
If (-not $ModPresent -or $ModPresent -lt 21) {
Write-Output 'INFO: INSTALL: Downloading and installing the SQL Server PowerShell module'
Try {
Install-Module 'SqlServer' -AllowClobber -Force -ErrorAction Stop
} Catch [System.Exception] {
Write-Output "ERROR: Failed to download and install the SQL Server PowerShell module $_"
Exit 1
}
}
if ($OnWindows) {
# The registry test does not seem to work for all versions of SSMS, so also perform additional test for actual installed ssms.exe
$SSMSPresent = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction SilentlyContinue | Where-Object { $_.DisplayName -eq 'SQL Server Management Studio' }
If (-not $SSMSPresent) {
$ExecPresent = Get-ChildItem "C:\Program Files (x86)\Microsoft SQL Server Management Studio *\Common7\IDE\ssms.exe"
if (-not $ExecPresent) {
Write-Output 'INFO: INSTALL: Installing SQL Server Management Studio'
$TmpFile = $Env:TEMP + '\SSMS-Setup.exe'
(New-Object -TypeName 'System.Net.WebClient').DownloadFile('https://aka.ms/ssmsfullsetup', $TmpFile)
$ArgumentList = '/Quiet'
Try {
$Process = Start-Process -FilePath $TmpFile -ArgumentList $ArgumentList -NoNewWindow -PassThru -Wait -ErrorAction Stop
} Catch [System.Exception] {
Write-Output "ERROR: Failed to install SQL Server Management Studio $_"
Exit 1
}
}
}
}
}
Function GenerateDDL-DB {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)][Microsoft.SqlServer.Management.Smo.Database]$Database,
[Parameter(Mandatory = $true)][String]$DestinationFilePath
)
$DDLFilename="$($DestinationFilePath)$FilenameSeparator$($Database.Name)$($DDLTag).sql"
$DDLFilenameTmp="$($DDLFilename).tmp"
# create output files
Try {
if (Test-Path $DDLFilename) {
Remove-Item $DDLFilename -ErrorAction Stop
}
} Catch [System.Exception] {
Write-Output "ERROR: Failed to delete file '$DDLFilename': $_"
Exit 1
}
Try {
$Null = New-Item -Path $DDLFilename -ItemType 'File' -ErrorAction Stop
} Catch [System.Exception] {
Write-Output "ERROR: Failed to create file '$DDLFilename': $_"
Exit 1
}
Try {
if (Test-Path $DDLFilenameTmp) {
Remove-Item $DDLFilenameTmp -ErrorAction Stop
}
} Catch [System.Exception] {
Write-Output "ERROR: Failed to delete file '$DDLFilenameTmp': $_"
Exit 1
}
Try {
$Null = New-Item -Path $DDLFilenameTmp -ItemType 'File' -ErrorAction Stop
} Catch [System.Exception] {
Write-Output "ERROR: Failed to create file '$DDLFilenameTmp': $_"
Exit 1
}
# do the real work
Write-Output "INFO: Generating DDL for database $Database to $DDLFilename ..."
# Explicitly adding CREATE DATABASE since it's not clear how to force that to be generated
# ALTER DATABASE statements are also not generated
Write-Output "-- Generated by $ThisProgram at $((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) for server $ServerName`n`nUSE master`ngo`nCREATE DATABASE $Database`ngo`nUSE $Database`ngo`n" | Out-File -FilePath $DDLFilename
$Transfer = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Transfer' $Database
$Transfer.Options.ScriptBatchTerminator = $true
$Transfer.Options.ContinueScriptingOnError = $true
$Transfer.Options.Filename = $DDLFilenameTmp
$Transfer.Options.ToFileOnly = $true
$Transfer.Options.AppendToFile = $true
$Transfer.Options.Indexes = $true
$Transfer.Options.ClusteredIndexes = $true
$Transfer.Options.NonClusteredIndexes = $true
$Transfer.Options.ColumnStoreIndexes = $true
$Transfer.Options.FullTextIndexes = $true
$Transfer.Options.FullTextCatalogs = $true
$Transfer.Options.FullTextStopLists = $true
$Transfer.Options.ExtendedProperties = $true
$Transfer.Options.Triggers = $true
$Transfer.Options.ScriptOwner = $true
$Transfer.Options.Permissions = $true
$Transfer.Options.SchemaQualify = $true
$Transfer.Options.SchemaQualifyForeignKeysReferences = $true
$Transfer.Options.Default = $true
$Transfer.Options.WithDependencies = $true
$Transfer.Options.ScriptSchema = $true
$Transfer.Options.IncludeDatabaseContext = $true
$Transfer.Options.IncludeHeaders = $true
$Transfer.Options.DRIAll = $true
$Transfer.Options.DriPrimaryKey = $true
$Transfer.Options.DriForeignKeys = $true
$Transfer.Options.DriUniqueKeys = $true
$Transfer.Options.DriClustered = $true
$Transfer.Options.DriNonClustered = $true
$Transfer.Options.DriChecks = $true
$Transfer.Options.DriDefaults = $true
$Transfer.Options.DriIndexes = $true
$Transfer.Options.DriAllKeys = $true
$Transfer.Options.DriAllConstraints = $true
$Transfer.Options.DriAll = $true
$Transfer.Options.Encoding = [System.Text.Encoding]::UTF8;
$Transfer.CreateTargetDatabase = $true
$Transfer.CopyAllLogins = $true
$Transfer.PreserveLogins = $true
$Transfer.CopyAllRoles = $true
$Transfer.CopyAllUsers = $true
$Transfer.CopyAllObjects = $true
$Transfer.PreserveDbo = $true
Try {
$Transfer.ScriptTransfer() | select-string "USE " -Notmatch
} Catch [System.Exception] {
Write-Output "ERROR: Error in \$Transfer.ScriptTransfer() for database $Database to $DDLFilenameTmp : $_"
Remove-Item $DDLFilename
Remove-Item $DDLFilenameTmp
Return
}
Get-Content -Path $DDLFilenameTmp | Add-Content -Path $DDLFilename
# Get some info on the SQL Server instance to include in the Babelfisn Compass report
$SrvResourceTag = "SMO_DDL SQL Server info:" # do not change this string, or the variable name below!
$SQLSrvName = $($MyServer.NetName)
Write-Output "`n--SQL Server Information generated by SMO_DDL.ps1 (do not change these lines):`n" | Add-Content -Path $DDLFilename
Write-Output "DECLARE @srvinfo VARCHAR(MAX)" | Add-Content -Path $DDLFilename
Write-Output "SET @srvinfo = '$SrvResourceTag $($SQLSrvName):Edition=$($MyServer.Edition)'" | Add-Content -Path $DDLFilename
Write-Output "SET @srvinfo = '$SrvResourceTag $($SQLSrvName):Processors=$($MyServer.Processors)'" | Add-Content -Path $DDLFilename
Write-Output "SET @srvinfo = '$SrvResourceTag $($SQLSrvName):VersionString=$($MyServer.VersionString)'" | Add-Content -Path $DDLFilename
Write-Output "SET @srvinfo = '$SrvResourceTag $($SQLSrvName):PhysicalMemory=$($MyServer.PhysicalMemory) MB'" | Add-Content -Path $DDLFilename
Write-Output "go" | Add-Content -Path $DDLFilename
Write-Output " " | Add-Content -Path $DDLFilename
# Generate the DDL
Write-Output "-- End of script generated by $ThisProgram `n`n" | Add-Content -Path $DDLFilename
Remove-Item $DDLFilenameTmp
# do not change the the following line:
Write-Output "INFO: Database [$($Database.Name)] : scripted to $DDLFilename"
}
#==================================================
# Main
#==================================================
#Write-Output "INFO: BabelfishCompassAutoDDL start PS"
# Older PS versions don't have $IsWindows etc.
$OnWindows = $false
$OnLinux = $false
$platform = [System.Environment]::OSVersion.Platform
If ($platform.ToString().substring(0,3) -eq "Win") {
$OnWindows = $true
$FilenameSeparator = "\";
} Else {
$OnLinux = $true
$FilenameSeparator = "/";
}
#Write-Output "INFO: BabelfishCompassAutoDDL/SMO: OnWindows:$OnWindows OnLinux:$OnLinux "
# validations
$Servername = $Servername.Trim()
if ($Servername -eq '') {
Write-Output "ERROR: -ServerName must be provided"
Exit 1
}
$Username = $Username.Trim()
if ($Username -eq '') {
Write-Output "ERROR: -Username must be provided"
Exit 1
}
$Password = $Password.Trim()
if ($Password -eq '') {
Write-Output "ERROR: -Password must be provided"
Exit 1
}
$OutputFolder = $OutputFolder.Trim()
if ($OutputFolder -eq '') {
Write-Output "ERROR: -OutputFolder must be provided"
Exit 1
}
if (-not (Test-Path $OutputFolder) ) {
Write-Output "ERROR: specified output folder not found [$OutputFolder]"
Exit 1
}
$Databases = $Databases.Trim()
if ([string]::Concat($Databases).Trim() -eq '') {
$Databases = "ALL"
Write-Output "INFO: no list of databases provided, using 'ALL'"
}
if ($DDLTag -eq '') {
$DDLTag = "_DDL_$(get-date -format yyyy-MM-dd)";
}
# validations done, now proceed...
Get-Prereqs
Import-Module SqlServer
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMOExtended") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Management.Common") | Out-Null
# create output dir
$SMOOutputDir = $SMOOutputDir.trim()
if ($SMOOutputDir -ne '') {
$DestinationFilePath = Join-Path -Path $OutputFolder -ChildPath $SMOOutputDir
}
else {
$DestinationFilePath = $OutputFolder
}
$TempDir = Test-Path -Path $DestinationFilePath -ErrorAction Stop
If (-not $TempDir) {
Try {
$Null = New-Item -Path $DestinationFilePath -ItemType 'Directory' -ErrorAction Stop
} Catch [System.Exception] {
Write-Output "ERROR: Failed to create directory '$DestinationFilePath' $_"
Exit 1
}
}
# connect to the SQL Server
$MyServer = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' $ServerName
$MyServer.ConnectionContext.LoginSecure = $false
$MyServer.ConnectionContext.Login = $Username
$MyServer.ConnectionContext.Password = $Password
If ($Null -eq $MyServer.Version) {
Write-Output "ERROR: Cannot connect to SQL Server $ServerName"
Exit 1
}
Write-Output "INFO: Connected to SQL Server $ServerName"
# use this to get indication of client-server roundtrip time
$StartTime = (Get-Date)
$DBchk = $Myserver.Databases | Where-Object { $_.name -eq 'master' }
$EndTime = (Get-Date)
$millisec=[Math]::Ceiling([int32](($EndTime - $StartTime).TotalMilliSeconds))
Write-Output "INFO: roundtrip millisec=$millisec" # do not change this string, must match Compass Java program
If ($DDLTag -eq 'report-roundtrip') { # do not change this string, must match Compass Java program
Exit 1
}
If ($Databases.ToUpper() -eq 'ALL') {
$DBs = $Myserver.Databases | Where-Object { $_.IsSystemObject -eq $False }
if ($DBs.Count -eq 0) {
Write-Output "ERROR: No user databases found in server '$ServerName'"
Exit 1
}
$DBList = [String]::Join(', ',$DBs)
Write-Output "INFO: $($DBs.Count) user databases found in server '$ServerName': $DBList"
If ($DDLTag -eq 'report-all-dbs') { # do not change this string, must match Compass Java program
# just report the databases that will be processed, and exit
Exit 1
}
Foreach ($DB in $DBs) {
If ($Database.ToLower() -eq "rdsadmin") { Continue }
GenerateDDL-DB -Database $DB -DestinationFilePath $DestinationFilePath
}
} Else {
$Databases = $Databases.Split(',')
Foreach ($Database in $Databases) {
$Database = $Database.Trim()
$db = $myServer.Databases[$Database]
If ($db.name -ne $Database) {
Write-Output "ERROR: Specified database not found: '$Database' in server '$ServerName' does not exist"
}
}
Foreach ($Database in $Databases) {
$Database = $Database.Trim()
$db = $myServer.Databases[$Database]
If ($db.name -eq $Database) {
GenerateDDL-DB -Database $db -DestinationFilePath $DestinationFilePath
}
}
}
#Write-Output "INFO: BabelfishCompassAutoDDL ready PS"
#
# end
#