-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScript-DBObjects.ps1
132 lines (113 loc) · 4.77 KB
/
Script-DBObjects.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
#
# Copyright Daniel Hutmacher under Creative Commons 4.0 license with attribution.
# http://creativecommons.org/licenses/by/4.0/
#
# Usage: Script-DBObjects -Server "server\instance" -DatabaseName "db" -Path "c:\temp"
#
Import-Module SqlServer
function Script-DBObjects([string]$Server, [string]$DatabaseName, [string]$Path) {
# Check if a folder exists, and create it (including the whole path) if it doesn't.
function Create-Folder([string]$path) {
$global:foldPath = $null
foreach($foldername in $path.split("\")) {
$global:foldPath += ($foldername+"\")
if (!(Test-Path $global:foldPath)){
New-Item -ItemType Directory -Path $global:foldPath | Out-Null
}
}
}
# Source: https://flamingkeys.com/convert-camel-case-to-space-delimited-display-name-with-powershell/
function Format-CamelCase([string]$inString) {
$newString = ""
$stringChars = $inString.GetEnumerator()
$charIndex = 0
foreach ($char in $stringChars) {
# If upper and not first character, add a space
if ([char]::IsUpper($char) -eq "True" -and $charIndex -gt 0) {
$newString = $newString + " " + $char.ToString()
} elseif ($charIndex -eq 0) {
# If the first character, make it a capital always
$newString = $newString + $char.ToString().ToUpper()
} else {
$newString = $newString + $char.ToString()
}
$charIndex++
}
$newString.ToString()
}
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO.Server") | Out-Null
$SMOserver = New-Object ("Microsoft.SqlServer.Management.SMO.Server") -argumentlist $Server
$Database = $SMOserver.databases[$DatabaseName]
$ScriptOptions = New-Object ("Microsoft.SqlServer.Management.Smo.ScriptingOptions")
#$ScriptOptions.ContinueScriptingOnError = $true
$ScriptOptions.ScriptOwner = $true
$ScriptOptions.ToFileOnly = $true
$ScriptOptions.AppendToFile = $false
$ScriptOptions.AnsiPadding = $true
$ScriptOptions.Bindings = $true
$ScriptOptions.ChangeTracking = $true
$ScriptOptions.DriAll = $true
#$ScriptOptions.EnforceScriptingOptions = $true
$ScriptOptions.ExtendedProperties = $true
$ScriptOptions.FullTextCatalogs = $true
$ScriptOptions.FullTextIndexes = $true
$ScriptOptions.FullTextStopLists = $true
$ScriptOptions.IncludeDatabaseRoleMemberships = $true
$ScriptOptions.IncludeFullTextCatalogRootPath = $true
$ScriptOptions.IncludeHeaders = $false
$ScriptOptions.Indexes = $true
$ScriptOptions.LoginSid = $true
$ScriptOptions.Permissions = $false
$ScriptOptions.SchemaQualify = $true
$ScriptOptions.SchemaQualifyForeignKeysReferences = $true
$ScriptOptions.ScriptBatchTerminator = $true
$ScriptOptions.ScriptDataCompression = $true
$ScriptOptions.ScriptOwner = $false
$ScriptOptions.ScriptSchema = $true
$ScriptOptions.Statistics = $false
$ScriptOptions.Triggers = $false
$ScriptOptions.XmlIndexes = $true
# These are the objects we want to script:
$Objects = $Database.Schemas
$Objects += $Database.PartitionFunctions
$Objects += $Database.PartitionSchemes
$Objects += $Database.Sequences
$Objects += $Database.Tables
$Objects += $Database.Views
$Objects += $Database.UserDefinedFunctions
$Objects += $Database.UserDefinedTypes
$Objects += $Database.UserDefinedTableTypes
$Objects += $Database.StoredProcedures
# Loop through each object:
foreach ($Object in $Objects | where { ($_.Schema -eq $null) -or !($_.IsSystemObject)}) {
if ($Object -ne $null) {
$TypeName = Format-CamelCase ($Object.GetType().Name+"s")
# Build folder structure
if ($Object.Schema -eq $null) {
$Folder=$path+"\"+$TypeName
} else {
$Folder=$path+"\"+$Object.Schema+"\"+$TypeName
}
# Create the folder if it doesn't exist:
Create-Folder $Folder
# File name:
$ScriptFile = $Object.Name
"$Folder\$ScriptFile.sql"
$ScriptOptions.FileName="$Folder\$ScriptFile.sql"
# Script the object:
$Object.Script($ScriptOptions)
# Separate the triggers from the regular tables/views:
foreach($trigger in $Object.Triggers) {
# Folder structure for triggers:
$Folder=$path+"\"+$Object.Schema+"\Triggers"
Create-Folder $Folder
# File name:
$ScriptFile = $trigger.Name
"$Folder\$ScriptFile.sql"
$ScriptOptions.FileName="$Folder\$ScriptFile.sql"
# Out you go:
$trigger.Script($ScriptOptions)
}
}
}
}