Repository navigation
Expand file tree
/
Copy path01_Server_Startup_Procs.ps1
More file actions
187 lines (149 loc) · 5.37 KB
/
Copy path01_Server_Startup_Procs.ps1
File metadata and controls
187 lines (149 loc) · 5.37 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
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
<#
.SYNOPSIS
Gets any Stored Procedures in the Master Database from the target server
These procs automatially run when the Instance starts up
SSIS_Cleanup and DQS install some startup procs here
.DESCRIPTION
Writes out any Stored Procedure from Master into SQL files in the "01 - Server Startup Procs" folder
.EXAMPLE
01_Server_Startup_Procs.ps1 localhost
.EXAMPLE
01_Server_Startup_Procs.ps1 server01 sa password
.Inputs
ServerName, [SQLUser], [SQLPassword]
.Outputs
.NOTES
.LINK
https://github.com/gwalkey
#>
[CmdletBinding()]
Param(
[string]$SQLInstance='localhost',
[string]$myuser,
[string]$mypass
)
# Load Common Modules and .NET Assemblies
try
{
Import-Module ".\SQLTranscriptase.psm1" -ErrorAction Stop
}
catch
{
Throw('SQLTranscriptase.psm1 not found')
}
LoadSQLSMO
# Init
Set-StrictMode -Version latest;
[string]$BaseFolder = (Get-Item -Path ".\" -Verbose).FullName
Write-Host -f Yellow -b Black "01 - Server Startup Stored Procedures"
Write-Output("Server: [{0}]" -f $SQLInstance)
function CopyObjectsToFiles($objects, $outDir) {
if (-not (Test-Path $outDir)) {
[System.IO.Directory]::CreateDirectory($outDir) | out-null
}
foreach ($o in $objects) {
if ($o -ne $null) {
$schemaPrefix = ""
if ($o.Schema -ne $null -and $o.Schema -ne "") {
$schemaPrefix = $o.Schema + "."
}
$fixedOName = $o.name.replace('\','_')
$scripter.Options.FileName = $outDir + $schemaPrefix + $fixedOName + ".sql"
$scripter.EnumScript($o)
}
}
}
# Server connection check
$SQLCMD1 = "select serverproperty('productversion') as 'Version'"
try
{
if ($mypass.Length -ge 1 -and $myuser.Length -ge 1)
{
Write-Output "Testing SQL Auth"
$myver = ConnectSQLAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $SQLCMD1 -User $myuser -Password $mypass -ErrorAction Stop| select -ExpandProperty Version
$serverauth="sql"
}
else
{
Write-Output "Testing Windows Auth"
$myver = ConnectWinAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $SQLCMD1 -ErrorAction Stop | select -ExpandProperty Version
$serverauth = "win"
}
if($myver -ne $null)
{
Write-Output ("SQL Version: {0}" -f $myver)
}
}
catch
{
Write-Host -f red "$SQLInstance appears offline."
Set-Location $BaseFolder
exit
}
[int]$ver = GetSQLNumericalVersion $myver
# New Up SMO Object
if ($serverauth -eq "win")
{
$srv = New-Object "Microsoft.SqlServer.Management.SMO.Server" $SQLInstance
$scripter = New-Object ("Microsoft.SqlServer.Management.SMO.Scripter") ($SQLInstance)
}
else
{
$srv = New-Object "Microsoft.SqlServer.Management.SMO.Server" $SQLInstance
$srv.ConnectionContext.LoginSecure=$false
$srv.ConnectionContext.set_Login($myuser)
$srv.ConnectionContext.set_Password($mypass)
$scripter = New-Object ("Microsoft.SqlServer.Management.SMO.Scripter") ($srv)
}
$db = New-Object ("Microsoft.SqlServer.Management.SMO.Database")
$tbl = New-Object ("Microsoft.SqlServer.Management.SMO.Table")
# Set scripter options to ensure only data is scripted
$scripter.Options.ScriptSchema = $true;
$scripter.Options.ScriptData = $false;
# Add your favorite options from
# https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.scriptingoptions.aspx
# https://www.simple-talk.com/sql/database-administration/automated-script-generation-with-powershell-and-smo/
$scripter.Options.AllowSystemObjects = $false
$scripter.Options.AnsiFile = $true
$scripter.Options.ClusteredIndexes = $true
$scripter.Options.DriAllKeys = $true
$scripter.Options.DriForeignKeys = $true
$scripter.Options.DriChecks = $true
$scripter.Options.DriPrimaryKey = $true
$scripter.Options.DriUniqueKeys = $true
$scripter.Options.DriWithNoCheck = $true
$scripter.Options.DriAllConstraints = $true
$scripter.Options.DriIndexes = $true
$scripter.Options.DriClustered = $true
$scripter.Options.DriNonClustered = $true
$scripter.Options.EnforceScriptingOptions = $true
$scripter.Options.ExtendedProperties = $true
$scripter.Options.FullTextCatalogs = $true
$scripter.Options.FullTextIndexes = $true
$scripter.Options.FullTextStopLists = $true
$scripter.Options.IncludeFullTextCatalogRootPath= $true
$scripter.Options.IncludeHeaders = $false
$scripter.Options.IncludeDatabaseRoleMemberships= $true
$scripter.Options.Indexes = $true
$scripter.Options.NoCommandTerminator = $false;
$scripter.Options.NonClusteredIndexes = $true
$scripter.Options.NoTablePartitioningSchemes = $false
$scripter.Options.Permissions = $true
$scripter.Options.SchemaQualify = $true
$scripter.Options.SchemaQualifyForeignKeysReferences = $true
$scripter.Options.ToFileOnly = $true
$scripter.Options.XmlIndexes = $true
Write-Output "Starting Export..."
$sqlDatabase = $srv.Databases['Master']
# Script out objects for each DB
$db = $sqlDatabase
$fixedDBName = $db.name.replace('[','')
$fixedDBName = $fixedDBName.replace(']','')
$output_path = "$BaseFolder\$SQLInstance\01 - Server Startup Procs\"
# Stored Procs
Write-Output "$fixedDBName - Stored Procs"
$storedProcs = $db.StoredProcedures | Where-object {-not $_.IsSystemObject }
CopyObjectsToFiles $storedProcs $output_path
Write-Output ("{0} Startup Stored Procs Exported" -f @($storedProcs).count)
# Return To Base
set-location $BaseFolder